Forum Discussion

Poloscopie's avatar
Poloscopie
Frequent Visitor
1 year ago
Solved

Issue with quantity difference between two dates when sorting by date descending

Hello dear PBI community,

 

I have an issue with a metric on a report, which calculates the difference of a quantity between two snapshots. This issue occurs when I change the way the snapshots are sorted.

 

The delta is calculated this way:

 

Qty Δ D-1 =
    VAR PreviousPhoto =
        CALCULATE(
            SUM('Evolution of Qty'[Qty]),
            PREVIOUSDAY('Evolution of Qty'[Photo])
        )
    RETURN
        IF(
            ISBLANK(PREVIOUSDAY('Evolution of Qty'[Photo])) , BLANK(),
            SUM('Evolution of Qty'[Qty]) - PreviousPhoto
        )

 

Originally, the formula works as we can see on the screenshot below. The "Qty Δ D-1" returns indeed for each category if we have more or less quantity than the day before.

 

My concern now is to display the matrix in a different order: I need the dates to be displayed from the newest to oldest (so in the example, from December 2nd to November 28th). But if it's possible to sort a chart by axe, it's not possible to sort a matrix by column.

So I created an extra column making the difference between the current date and the photo date in order to use this column to sort the Photo column with it.

 

But this sorting, even if the columns order is now OK, my metric is now broken, as displayed below:

The measure returns the quantity value, meaning that the PreviousPhoto var always = 0.

 

Trying to change PREVIOUSDAY by NEXTDAY does not fix the function. Actually, we can see that DAX still interprets PREVIOUSDAY well as the value for the oldest photo date returns a blank value.

 

I don't know how to fix this, nothing seems to work.

  • lbendlin's avatar
    lbendlin
    1 year ago

    I missed the wrong PREVIOUSDAY reference

     

     

10 Replies

  • Hello Poloscopie,

     

    Can you please try this approach:

    Qty Δ D-1 = 
    VAR CurrentDate = MAX('Evolution of Qty'[Photo])
    VAR PreviousDate =
        CALCULATE(
            MAX('Evolution of Qty'[Photo]),
            'Evolution of Qty'[Photo] < CurrentDate
        )
    VAR PreviousQty =
        CALCULATE(
            SUM('Evolution of Qty'[Qty]),
            'Evolution of Qty'[Photo] = PreviousDate
        )
    RETURN
        IF(
            ISBLANK(PreviousDate),
            BLANK(),
            SUM('Evolution of Qty'[Qty]) - PreviousQty
        )
    
    • Poloscopie's avatar
      Poloscopie
      Frequent Visitor

      Unfortunately, this gives the same result.

      I named your function "Qty Δ Test".

      When dates are sorted the ascending way, it's ok:

      When sorted the other way, your function returns blank:

       

  • Your data model is missing a calendar table - those are mandatory if you want to use time intelligence functions.