Forum Discussion

omustu's avatar
omustu
Frequent Visitor
1 year ago
Solved

Number sorting not working properly in calculated measure

Hello everyone, I have a calculated measure with the following DAX Formula: Measure1 = VAR max_date = [LatestDate] VAR min_date = [SecondLatestDate] VAR min_status = CALCULATE(     MIN('...
  • Anonymous's avatar
    Anonymous
    1 year ago

    Hi omustu 

     

    According to your pbix screenshots, I think it may be caused by the Cost_Saving measure having blank subtotals in the matrix. Power BI may fail to sort if the measure is blank in certain rows or columns when sorting.
    When I added the subtotal calculations to your measure, the sorting started working correctly.

    Cost_Saving = 
    VAR max_date = [LatestDate]
    VAR min_date = [SecondLatestDate]
    VAR min_status = CALCULATE(
        MIN(Sheet1[Status]),
        FILTER(Sheet1, Sheet1[Date] = min_date && Sheet1[UID] = SELECTEDVALUE(Sheet1[UID]))
    )
    VAR max_status = CALCULATE(
        MIN(Sheet1[Status]),
        FILTER(Sheet1, Sheet1[Date] = max_date && Sheet1[UID] = SELECTEDVALUE(Sheet1[UID]))
    )
    VAR idea_number = SELECTEDVALUE(Sheet1[Idea Number])
    VAR total_savings = CALCULATE(
        SUM(Sheet1[Saving Cost]),
        FILTER(
            ALL(Sheet1),
            Sheet1[Date] = max_date && Sheet1[Idea Number] = idea_number
        )
    )
    VAR is_total = HASONEVALUE(Sheet1[UID]) = FALSE() || HASONEVALUE(Sheet1[Idea Number]) = FALSE()
    RETURN
    IF(
        is_total,
        total_savings,
        IF(min_status <> BLANK() && max_status <> BLANK() && min_status <> max_status, total_savings)
    )

     

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