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
pitucc
Helper I
Helper I

DoD Variation column using Dateaddd on Business Days

Hi, 

Here is the kind of data set i have : 

DateProduct Price Rank
22/08/2017A95.501
22/08/2017B61.111
22/08/2017C65.741
21/08/2017A49.612
21/08/2017B60.612
21/08/2017C52.302
20/08/2017A75.133
20/08/2017B91.193
20/08/2017C85.003
16/08/2017A37.844
16/08/2017B99.514
16/08/2017C94.634

 

I 'm trying to compute the Day on Day price variation for each product. I managed to have something that works only if i have data on each days (but i will not have always daily datas, most of the time, nothing on weekend and sometimes one data points can miss on week days also)

Here is my column formula :

 

DoD Price= CALCULATE(
     ((AVERAGE(DataBase[Price]))-CALCULATE(
         AVERAGE(DataBase[Price]);DATEADD(DataBase[Date];-1;DAY);
         ALLEXCEPT(DataBase;DataBase[Date];DataBase[Product])));
     ALLEXCEPT(DataBase;DataBase[Date];DataBase[Product])
)

I was thinking about using the "Rank" column wich  is computed with this formula : 

Rank = RANKX(DataBase;DataBase[Date];;DESC;Dense)

 

 

But i don't know if and how i can use to compute somthing like 

 

CALCULATE (
                        AVERAGE(DataBase[Price])  - 
                        CALCULATE( AVERAGE(DataBase[Price]);RANK=RANK+1)) 

since the Rank+1 will always existe except for the oldest value. 

 

 

 

Many thanks for your help

Regards

Pierre

2 ACCEPTED SOLUTIONS

Hi @pitucc,

 

My formula is a measure, if you direct use it as calculate column ,it may caused the issue.

In addition, if you need you can also use below formula: (calculate column version)

 

DoD diff =
VAR previous_date =
    MAXX ( FILTER ( ALL ( 'Sample' ), [Date] < EARLIER ( [Date] ) ), [Date] )
RETURN
    IF (
        previous_date <> 0,
        AVERAGEX (
            FILTER (
                ALL ( 'Sample' ),
                [Product] = EARLIER ( [Product] )
                    && [Date] = EARLIER ( [Date] )
            ),
            [Price]
        )
            - AVERAGEX (
                FILTER (
                    ALL ( 'Sample' ),
                    [Product] = EARLIER ( [Product] )
                        && [Date] = previous_date
                ),
                [Price]
            ),
        0
    )

12.PNG

 

 

Regards,

Xiaoxin Sheng

Community Support Team _ Xiaoxin
If this post helps, please consider accept as solution to help other members find it more quickly.

View solution in original post

Hello,

I just wnated to close the topic, to compute a variation for anything alse than day on day, in the computation of "previous_avg" just replace "current_date" by " (current_date-XX) where XX is the lag you want for your variation. 

For a WoW variation I have used the code below : 

WoW Cheap = 
VAR current_Product =LASTNONBLANK ( DataBase[Name]; [Name] )
VAR current_date = MAX ( DataBase[ValuationDate] )
VAR current_avg =
    AVERAGEX (
        FILTER (
            ALL ( DataBase );
            [Name] = current_Product
                && [ValuationDate] = current_date
        );
        [Cheap]
    )
VAR previous_avg =
    AVERAGEX (
        FILTER (
            ALL ( DataBase );
            [Name] = current_Product
                && [ValuationDate]
                    = MAXX (
                        FILTER (
                            ALL ( DataBase );
                            [Name] = current_Product
                                && [ValuationDate] < (current_date-6)
                        );
                        [ValuationDate]
                    )
        );
        [Cheap]
    )
RETURN
    IF ( previous_avg <> 0; current_avg - previous_avg; 0 )

Many thanks once again for your help ! 

 

View solution in original post

7 REPLIES 7
Greg_Deckler
Super User
Super User

So, in the case of a missing date, do you want to calculate from the last date that there is?

 

For example:

 

22/08/2017, A, 95.50

24/08/2017, A, 95.60

 

So, on 23/08/2017 the price difference should be 0 and on 24/08/2017 the price difference should be .10 (not 95.60)?


@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...

exactly yes !  That's why i was thinking about using the "rank" column. 

Hi @pitucc,

 

If you want to calculate day of day moving average diff, you can try to use below formula:

DoD Avg Diff = 
VAR current_Product =LASTNONBLANK ( 'Sample'[Product], [Product] )
VAR current_date = MAX ( 'Sample'[Date] )
VAR current_avg =
    AVERAGEX (
        FILTER (
            ALL ( 'Sample' ),
            [Product] = current_Product
                && [Date] = current_date
        ),
        [Price]
    )
VAR previous_avg =
    AVERAGEX (
        FILTER (
            ALL ( 'Sample' ),
            [Product] = current_Product
                && [Date]
                    = MAXX (
                        FILTER (
                            ALL ( 'Sample' ),
                            [Product] = current_Product
                                && [Date] < current_date
                        ),
                        [Date]
                    )
        ),
        [Price]
    )
RETURN
    IF ( previous_avg <> 0, current_avg - previous_avg, 0 )

3.PNG

 

Regards,

Xiaoxin Sheng

Community Support Team _ Xiaoxin
If this post helps, please consider accept as solution to help other members find it more quickly.

Hello,

Thanks for your help but I have litterally copy pasted your formula but it don't have the same result as you do... 

 

Capture.PNG

 

Any idea why ? 

Furthermore, is the formula easely adaptable to compte a Month on month variation difference ? 

 

Thanks

 

Hi @pitucc,

 

My formula is a measure, if you direct use it as calculate column ,it may caused the issue.

In addition, if you need you can also use below formula: (calculate column version)

 

DoD diff =
VAR previous_date =
    MAXX ( FILTER ( ALL ( 'Sample' ), [Date] < EARLIER ( [Date] ) ), [Date] )
RETURN
    IF (
        previous_date <> 0,
        AVERAGEX (
            FILTER (
                ALL ( 'Sample' ),
                [Product] = EARLIER ( [Product] )
                    && [Date] = EARLIER ( [Date] )
            ),
            [Price]
        )
            - AVERAGEX (
                FILTER (
                    ALL ( 'Sample' ),
                    [Product] = EARLIER ( [Product] )
                        && [Date] = previous_date
                ),
                [Price]
            ),
        0
    )

12.PNG

 

 

Regards,

Xiaoxin Sheng

Community Support Team _ Xiaoxin
If this post helps, please consider accept as solution to help other members find it more quickly.

That's perfect many thanks !

Is it easely adaptable for MoM variation or 21days on 21 days variation ? 

Oh and one last question, what is difference between using your solution as column formula or as a measure ? 

 

Thanks

 

Hello,

I just wnated to close the topic, to compute a variation for anything alse than day on day, in the computation of "previous_avg" just replace "current_date" by " (current_date-XX) where XX is the lag you want for your variation. 

For a WoW variation I have used the code below : 

WoW Cheap = 
VAR current_Product =LASTNONBLANK ( DataBase[Name]; [Name] )
VAR current_date = MAX ( DataBase[ValuationDate] )
VAR current_avg =
    AVERAGEX (
        FILTER (
            ALL ( DataBase );
            [Name] = current_Product
                && [ValuationDate] = current_date
        );
        [Cheap]
    )
VAR previous_avg =
    AVERAGEX (
        FILTER (
            ALL ( DataBase );
            [Name] = current_Product
                && [ValuationDate]
                    = MAXX (
                        FILTER (
                            ALL ( DataBase );
                            [Name] = current_Product
                                && [ValuationDate] < (current_date-6)
                        );
                        [ValuationDate]
                    )
        );
        [Cheap]
    )
RETURN
    IF ( previous_avg <> 0; current_avg - previous_avg; 0 )

Many thanks once again for your help ! 

 

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.