Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
RobRayborn
Helper III
Helper III

difference from previous Friday

I need to calculate the difference between the amount taken on a day and the amount taken on the previous day.  The problem is that on Satruday and Sunday the amount doesn't change, so on Monday my difference ends up being Zero. So I end up with only four days. 
I need to subtract Monday from the previous Friday amount.  
I have a standard Date Table with offsets and "IsWeekday" type columns. 

The blue highlight below shows the amount on prevous Friday. The yellow highlight shows the % amount diff from previous day, or DATEADD, -1, DAY.

Screenshot 2023-03-06 160241.jpg 

1 ACCEPTED SOLUTION
Jihwan_Kim
Super User
Super User

Hi,

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

There are some ways to achieve this, and I think it depends on how the business process for Saturday and Sunday is defined. I assume there are blank (not zero) data on Sat and Sun, and one of ways to achieve this is using lastnonblank DAX function.

Jihwan_Kim_1-1678163509273.png

 

 

Jihwan_Kim_0-1678163477431.png

 

Diff %: =
VAR _currentdate =
    MAX ( 'Calendar'[Date] )
VAR _lastnonblank =
    CALCULATE (
        LASTNONBLANK ( 'Calendar'[Date], [Value sum:] ),
        FILTER ( ALL ( 'Calendar'[Date] ), 'Calendar'[Date] < _currentdate )
    )
VAR _lastnonblankvalue =
    CALCULATE ( [Value sum:], 'Calendar'[Date] = _lastnonblank )
RETURN
    IF (
        NOT ISBLANK ( [Value sum:] ) && HASONEVALUE ( 'Calendar'[Date] ),
        DIVIDE ( [Value sum:] - _lastnonblankvalue, _lastnonblankvalue )
    )

 

If this post helps, then please consider accepting it as the solution to help other members find it faster, and give a big thumbs up.


Go to My LinkedIn Page


View solution in original post

5 REPLIES 5
RobRayborn
Helper III
Helper III

Thank you so much for the assitance!  This worked perfectly.

FreemanZ
Super User
Super User

hi @RobRayborn 

apparently Jihwan's solution works. 

 

I tried to simulate your case with such data:

FreemanZ_0-1678167327415.png

 

verified by creating a table visual with data, value columns and a measure like:

measure = 
VAR _date = MAX(TableName[date])
VAR _value =
MAXX(
    FILTER(
        ALL(TableName),
        TableName[date] = _date
    ),
    TableName[value]
)
VAR _datepre =
MAXX(
    FILTER(
        ALL(TableName),
        TableName[date] < _date
    ),
    TableName[date]
)
VAR _valuepre =
MAXX(
    FILTER(
        ALL(TableName),
        TableName[date] = _datepre
    ),
    TableName[value]
)
RETURN
DIVIDE(_value, _valuepre) -1

 

or like:

measure2 = 
VAR _date = MAX(TableName[date])
VAR _value =
MAXX(
    FILTER(
        ALL(TableName),
        TableName[date] = _date
    ),
    TableName[value]
)
VAR _valuepre =
MAXX(
    TOPN(
        1, 
        FILTER(
            ALL(TableName),
            TableName[date] < _date
        ),
        TableName[date]
    ),
    TableName[value]
)
RETURN
DIVIDE(_value, _valuepre) -1

 

it worked like:

FreemanZ_1-1678167468790.png

 

if you also accept calculated columns, try like:

column = 
VAR _datepre =
MAXX(
    FILTER(
        ALL(TableName),
        TableName[date] < EARLIER(TableName[date])
    ),
    TableName[date]
)
VAR _valuepre =
MAXX(
    FILTER(
        ALL(TableName),
        TableName[date] = _datepre
    ),
    TableName[value]
)
RETURN
DIVIDE([value], _valuepre) -1

or 

column2 = 
VAR _valuepre =
MAXX(
    TOPN(
        1, 
        FILTER(
            ALL(TableName),
            TableName[date] < EARLIER(TableName[date])
        ),        
        TableName[date]
    ),
   TableName[value]
)
RETURN
DIVIDE([value], _valuepre) -1

 

it worked like:

FreemanZ_2-1678167586001.png

Jihwan_Kim
Super User
Super User

Hi,

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

There are some ways to achieve this, and I think it depends on how the business process for Saturday and Sunday is defined. I assume there are blank (not zero) data on Sat and Sun, and one of ways to achieve this is using lastnonblank DAX function.

Jihwan_Kim_1-1678163509273.png

 

 

Jihwan_Kim_0-1678163477431.png

 

Diff %: =
VAR _currentdate =
    MAX ( 'Calendar'[Date] )
VAR _lastnonblank =
    CALCULATE (
        LASTNONBLANK ( 'Calendar'[Date], [Value sum:] ),
        FILTER ( ALL ( 'Calendar'[Date] ), 'Calendar'[Date] < _currentdate )
    )
VAR _lastnonblankvalue =
    CALCULATE ( [Value sum:], 'Calendar'[Date] = _lastnonblank )
RETURN
    IF (
        NOT ISBLANK ( [Value sum:] ) && HASONEVALUE ( 'Calendar'[Date] ),
        DIVIDE ( [Value sum:] - _lastnonblankvalue, _lastnonblankvalue )
    )

 

If this post helps, then please consider accepting it as the solution to help other members find it faster, and give a big thumbs up.


Go to My LinkedIn Page


This does what I asked for it to do, however it seems to have broken the TOTALYTD and DATESYTD measures.  I cannot produce a Rolling Annual amount with this solution.  Any ideas on how to fix this?

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.