Forum Discussion

Kate_McCulloch's avatar
Kate_McCulloch
Regular Visitor
1 year ago
Solved

Count based on disticnt value in another column

Hi Wondering if anyone could help,

 

I'm trying to do a total cost count for accommodation, but only want to count the value once based on the Family ID (distinct count)

 

If anyone could help I would really appreciate it

 

Thanks 

 

 

 

  • Kate_McCulloch , Try using a measure for this

     

    DAX
    TotalCostByFamilyID =
    SUMX(
    SUMMARIZE(
    YourTableName,
    YourTableName[Family_ID],
    "DistinctCost",
    MAX(YourTableName[Total Cost Accommodation])
    ),
    [DistinctCost]
    )

5 Replies

  • Kate_McCulloch , Try using a measure for this

     

    DAX
    TotalCostByFamilyID =
    SUMX(
    SUMMARIZE(
    YourTableName,
    YourTableName[Family_ID],
    "DistinctCost",
    MAX(YourTableName[Total Cost Accommodation])
    ),
    [DistinctCost]
    )

  • Hello Kate_McCulloch ,

     

    I could not totally grasp the question and a snapshot of the expected solution would have helped. I am assuming you are looking for the following format as the end result:

    Family_Id Total Cost Accomodation
    41284151130
    41284704075
    41284845283
    4128739525
    41295961792.27

     

    If the same value repeats for a particular Family_Id in the table, you could simply opt for an min/max/average at Family_Id level either using a measure as following or just dragging-and-dropping and then changing the aggregation style to Min/Max/Average.
    Unique Cost Accomodation = MIN(Table[Total Cost Accomodation])

    If there is more to the problem, please add some more context and share the end result you are expecting.

  • Hi Kate_McCulloch ,
    If you want to calculate the total accommodation cost, but count each Family ID only once (even if they appear multiple times in your data), you need a DAX measure that sums the cost just once per unique Family ID. The best way to do this in Power BI is to first summarize your table so that you get the maximum (or minimum, or average—whichever is appropriate for your business logic) accommodation cost per Family ID.

     

    Then, you sum up those distinct costs across all Family IDs. This ensures that even if a Family ID appears more than once in your table, its accommodation cost is only counted once in the total. The following DAX measure achieves this by summarizing your data by Family ID and then summing up the distinct costs:

    TotalCostByFamilyID =
    SUMX(
        SUMMARIZE(
            YourTableName,
            YourTableName[Family_ID],
            "DistinctCost",
            MAX(YourTableName[Total Cost Accommodation])
        ),
        [DistinctCost]
    )
    

     

  • Hi Kate_McCulloch,
    I add two more rows to the sample data to get the actual Count. Try with below DAX. 

    Total Cost Accommodation Count by Family = 
    COUNTROWS(
        DISTINCT(
            SELECTCOLUMNS('Table', "FamilyID", 'Table'[Family_ID], "Cost", 'Table'[Total Cost Accommodation])
                )
            )


    SampleData:


    Result:

     

    Thanks,
    If you found this solution helpful, please consider giving it a LikešŸ‘ and marking it as Accepted Solutionāœ”. This helps improve visibility for others who may be encountering/facing same questions/issues.