• Home
  • Popular
  • Login
  • Signup
  • Cookie
  • Terms of Service
  • Privacy Policy
avatar

Posted by Micro Bot


29 Nov, 2024

Updated at 05 Dec, 2024

Distinct count to match VATs and Years

Hi everyone!

I have a file with companies' VAT Numbers, the Financial Year, and their Net Sales during that year. It looks like this:

VAT NumberFinancial YearNet Sales
80022018100000
80032018200000
80042018null
80022019400000
80032019500000
80042019600000
800220200
80032020800000
80042020900000
800220211000000
800320211100000
800420211200000
800220221300000
800320221400000
800420221500000
800220230
800320231700000
800420231800000
800520231900000


What I would like to do is create a measure that sums the net sales for the following companies:
(1) Companies whose VAT Number appears in the data as many times as the financial years available (in this case 6 times, but it needs to be dynamic). 
-> Therefore the company with VAT 8005 should be excluded.
(2) Companies whose Net Sales are neither null nor 0 in 2023.
-> Therefore, the company with VAT 8002 should be excluded.

The issue that I am facing is that my formula only filters out the companies whose Net Sales are null or 0 during 2023, but IT DOES NOT FILTER OUT COMPANIES WHOSE VAT APPEARS ONLY FEW TIMES (like 8005 in this example). The Distinct Count is not working. There are two DAX measures that I am using but none is working:

(1)

Net Sales (Valid Companies 2023) =
VAR DistinctYearsCount = COUNTROWS(DISTINCT('Page1'[Financial Year]))  -- Count of distinct financial years
VAR ValidCompanies =
    FILTER(
        VALUES('Page1'[VAT Number]),  -- Iterate through each VAT Number
        -- Ensure this VAT appears in as many rows as there are distinct years
        CALCULATE(COUNTROWS('Page1')) = DistinctYearsCount
        &&
        -- Ensure no NULL or 0 Net Sales in 2023
        CALCULATE(
            COUNTROWS(
                FILTER(
                    'Page1',
                    ISBLANK('Page1'[Net Sales]) || 'Page1'[Net Sales] = 0
                )
            ),
            'Page1'[Financial Year] = 2023
        ) = 0
    )
RETURN
    SUMX(
        ValidCompanies,
        CALCULATE(SUM('Page1'[Net Sales]))
    )


(2)

Net Sales (Valid Companies) =
VAR DistinctYearsCount = COUNTROWS(DISTINCT('Page1'[Financial Year]))  -- Total distinct years
VAR ValidCompanies =
    FILTER(
        VALUES('Page1'[VAT Number]),  -- Iterate through each VAT Number
        -- Check if this VAT appears in all distinct financial years
        CALCULATE(DISTINCTCOUNT('Page1'[Financial Year])) = DistinctYearsCount
        &&
        -- Ensure no null or zero Net Sales in 2023
        CALCULATE(
            COUNTROWS(
                FILTER(
                    'Page1',
                    ISBLANK('Page1'[Net Sales]) || 'Page1'[Net Sales] = 0
                )
            ),
            'Page1'[Financial Year] = 2023
        ) = 0
    )
RETURN
    -- Aggregate Net Sales for valid companies
    SUMX(
        ValidCompanies,
        CALCULATE(SUM('Page1'[Net Sales]))
    )



I would really appreciate your help! Thank you very much in advance!