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
gvg
Post Prodigy
Post Prodigy

dax universal formula to calculate difference to previous period sales

Hi,

 

Is there a universal way to compare to previous period ? There are plenty of threads on how to compare to previous month or year, like this:

 

     sales_before = calculate(sum('Table'[Sales]), PARALLELPERIOD('Table'[Date],-1,MONTH)) 

 

But is it possible to have a universal formula, that works both on years, quarters, months, days depending on what filter is used? I.e. if I show in a chart Year1 and Year2, I'd like to calculate the difference between Year1 and Year2. If I double click on  Year1 to show quarters of that year, I'd like to see differences between the displayed quarters.

1 ACCEPTED SOLUTION

@gvg

 

Hi,

 

There is no such a function as far as I know. Maybe there is a workaround we can try.

  1. We create a hierarchy by ourselves. Every level is from a column of the Date Table.
  2. Create a measure like this. I am sure you have to change a lot. It just shows the way we can do.

 

Measure =
VAR CurrentSA = [Sales Amount]
RETURN
    IF (
        ISFILTERED ( 'Date'[Calendar Year] ),
        CurrentSA
            - CALCULATE ( [Sales Amount], PARALLELPERIOD ( 'Date'[Date], -1, YEAR ) ),
        IF (
            ISFILTERED ( 'Date'[Calendar Year Quarter] ),
            CurrentSA
                - CALCULATE ( [Sales Amount], PARALLELPERIOD ( 'Date'[Date], -1, QUARTER ) ),
            IF (
                ISFILTERED ( 'Date'[Calendar Year Month] ),
                CurrentSA
                    - CALCULATE ( [Sales Amount], PARALLELPERIOD ( 'Date'[Date], -1, MONTH ) ),
                IF (
                    ISFILTERED ( 'Date'[Date] ),
                    CurrentSA
                        - CALCULATE ( [Sales Amount], 'Date'[Date] - 1 )
                )
            )
        )
)

 

Note: 1. No filter can be applied.

          2. Use the Preview Matrix.

dax universal formula to calculate previous period sales2 .jpgdax universal formula to calculate previous period sales .jpg

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Best Regards!

Dale

Community Support Team _ Dale
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

View solution in original post

8 REPLIES 8
vanessafvg
Super User
Super User

@gvg are you referring to https://msdn.microsoft.com/en-us/library/ee634972.aspx  SAMEPERIODLASTYEAR?





If I took the time to answer your question and I came up with a solution, please mark my post as a solution and /or give kudos freely for the effort 🙂 Thank you!

Proud to be a Super User!




No. I am referring to previous period, not parallel period. If years 2017 and 2016 are charted, I want the difference between 2017 and 2016. If 2017 is drilled down to 2017-Q1 and 2017-Q2, I want the difference between those adjacent quarters. If 2017-Q1 is drilled down to 2017-Jan, 2017-Feb and 2017-Mar, I want the difference between the adjacent months.

@gvg

 

Hi,

 

There is no such a function as far as I know. Maybe there is a workaround we can try.

  1. We create a hierarchy by ourselves. Every level is from a column of the Date Table.
  2. Create a measure like this. I am sure you have to change a lot. It just shows the way we can do.

 

Measure =
VAR CurrentSA = [Sales Amount]
RETURN
    IF (
        ISFILTERED ( 'Date'[Calendar Year] ),
        CurrentSA
            - CALCULATE ( [Sales Amount], PARALLELPERIOD ( 'Date'[Date], -1, YEAR ) ),
        IF (
            ISFILTERED ( 'Date'[Calendar Year Quarter] ),
            CurrentSA
                - CALCULATE ( [Sales Amount], PARALLELPERIOD ( 'Date'[Date], -1, QUARTER ) ),
            IF (
                ISFILTERED ( 'Date'[Calendar Year Month] ),
                CurrentSA
                    - CALCULATE ( [Sales Amount], PARALLELPERIOD ( 'Date'[Date], -1, MONTH ) ),
                IF (
                    ISFILTERED ( 'Date'[Date] ),
                    CurrentSA
                        - CALCULATE ( [Sales Amount], 'Date'[Date] - 1 )
                )
            )
        )
)

 

Note: 1. No filter can be applied.

          2. Use the Preview Matrix.

dax universal formula to calculate previous period sales2 .jpgdax universal formula to calculate previous period sales .jpg

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Best Regards!

Dale

Community Support Team _ Dale
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Could you please share the PBIX file ?

I use below query but value is not changing for different QTR in Measures. it showing same as it is in USD_MNF

 

Measure =
VAR CurrentSA = [Sales Amount]
RETURN
IF (
ISFILTERED ( PADDS[Year] ),
CurrentSA
- CALCULATE ( [Sales Amount], PARALLELPERIOD ( PADDS[PERIOD], -1, YEAR ) ),
IF (
ISFILTERED ( PADDS[Quater] ),
CurrentSA
- CALCULATE ( [Sales Amount], PARALLELPERIOD ( PADDS[PERIOD], -1, QUARTER ) ),
IF (
ISFILTERED ( PADDS[Month] ),
CurrentSA
- CALCULATE ( [Sales Amount], PARALLELPERIOD ( PADDS[PERIOD], -1, MONTH ) ),
IF (
ISFILTERED ( PADDS[PERIOD] ),
CurrentSA
- CALCULATE ( [Sales Amount], PADDS[PERIOD] - 1 )
)
)
)
)

 

Capture.PNG

 

Anonymous
Not applicable

Same here.. 😞

Just throwing in another solution from an earlier thread, on essentially the same question:

Similar logic to @v-jiascu-msft 's answer above but based on day count of filtered period vs parallel period of a given type.

https://community.powerbi.com/t5/Desktop/COMPARISON-Current-Period-sum-Vs-Previous-Period-sum-Change...


Owen Auger
Did I answer your question? Mark my post as a solution!
Blog
Twitter
LinkedIn

@v-jiascu-msft  thanks!

 

 

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.