Forum Discussion

AshwiniPulipat's avatar
AshwiniPulipat
Frequent Visitor
2 years ago
Solved

Need help in writing DAX query formula

Hello, 

I am trying to write a Dax query for the below on e but the expected results are not coming. Could you guys help me in this.

I have sales data which contains the product codes, territory, state, city and sales amount. most product codes are sold in all territories but there are some product codes which are not sold in all territories.  I need to create a measure to calculate the percentage of productcodes which are not sold everywhere in territories.


Attaching the sample

Thank you

  • Hi,

    I am not sure how your semantic model looks like, but I tried to create a sample pbix file like below.

    Please check the below picture and the attached pbix file.

     

     

     

     

     

    % of product count not-all-territory: =
    VAR _result =
        COUNTROWS (
            FILTER (
                VALUES ( 'product'[product_code] ),
                CALCULATE ( COUNTROWS ( SUMMARIZE ( data, territory[territory] ) ) ) <> 4
            )
        )
    VAR _allproduct =
        COUNTROWS ( VALUES ( 'product'[product_code] ) )
    RETURN
        DIVIDE ( _result, _allproduct )
    

     

  • Anonymous's avatar
    Anonymous
    2 years ago

    Hi AshwiniPulipat ,
    Thank you Jihwan_Kim and aduguid  very much for the solution, and I've tried other ways to help you understand the problem:

    PercentageNotsoldEverywhere = 
    VAR _count1 =
        CALCULATE (
            DISTINCTCOUNT ( 'Table (2)'[Territory] ),
             ALLSELECTED ( 'Table (2)' )
        )
    VAR _count2 =
        CALCULATE (
            DISTINCTCOUNT ( 'Table (2)'[Territory] ),
            ALLEXCEPT ( 'Table (2)', 'Table (2)'[Prouduct code] )
        )
    RETURN
    1 - DIVIDE ( _count2, _count1 )
    

    You change to the new data source you need to use in the following actions.

    Hope it helps!

     

    Best regards,
    Community Support Team_ Tom Shen

     

    If this post helps then please consider Accept it as the solution to help the other members find it more quickly.

     

3 Replies

  • Hi,

    I am not sure how your semantic model looks like, but I tried to create a sample pbix file like below.

    Please check the below picture and the attached pbix file.

     

     

     

     

     

    % of product count not-all-territory: =
    VAR _result =
        COUNTROWS (
            FILTER (
                VALUES ( 'product'[product_code] ),
                CALCULATE ( COUNTROWS ( SUMMARIZE ( data, territory[territory] ) ) ) <> 4
            )
        )
    VAR _allproduct =
        COUNTROWS ( VALUES ( 'product'[product_code] ) )
    RETURN
        DIVIDE ( _result, _allproduct )
    

     

  • aduguid's avatar
    aduguid
    Icon for Memorable Member rankMemorable Member
    • Create a Calculated Table: First, create a calculated table that lists all the unique combinations of product codes and territories.

     

    ProductTerritoryCombination = 
    CROSSJOIN (
        DISTINCT ( SalesData[ProductCode] ),
        DISTINCT ( SalesData[Territory] )
    )

     

     

    • Count Total Product-Territory Combinations: Create a measure to count the total number of product-territory combinations.

     

    TotalProductTerritoryCombinations = 
    COUNTROWS ( ProductTerritoryCombination )

     

     

    • Count Actual Sales Combinations: Create a measure to count the actual number of product-territory combinations that have sales.

     

    ActualProductTerritoryCombinations = 
    COUNTROWS ( 
        SUMMARIZE (
            SalesData,
            SalesData[ProductCode],
            SalesData[Territory]
        )
    )

     

     

    • Calculate the Difference: Create a measure to calculate the difference between the total possible combinations and the actual sales combinations.

     

    MissingProductTerritoryCombinations = 
    [TotalProductTerritoryCombinations] - [ActualProductTerritoryCombinations]

     

     

    • Calculate the Percentage: Finally, create a measure to calculate the percentage of product codes that are not sold in all territories.

     

    PercentageMissingProductTerritoryCombinations = 
    DIVIDE (
        [MissingProductTerritoryCombinations],
        [TotalProductTerritoryCombinations]
    )

     

     

  • Anonymous's avatar
    Anonymous
    Not applicable

    Hi AshwiniPulipat ,
    Thank you Jihwan_Kim and aduguid  very much for the solution, and I've tried other ways to help you understand the problem:

    PercentageNotsoldEverywhere = 
    VAR _count1 =
        CALCULATE (
            DISTINCTCOUNT ( 'Table (2)'[Territory] ),
             ALLSELECTED ( 'Table (2)' )
        )
    VAR _count2 =
        CALCULATE (
            DISTINCTCOUNT ( 'Table (2)'[Territory] ),
            ALLEXCEPT ( 'Table (2)', 'Table (2)'[Prouduct code] )
        )
    RETURN
    1 - DIVIDE ( _count2, _count1 )
    

    You change to the new data source you need to use in the following actions.

    Hope it helps!

     

    Best regards,
    Community Support Team_ Tom Shen

     

    If this post helps then please consider Accept it as the solution to help the other members find it more quickly.