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
Quinnie2017
Helper II
Helper II

Period to Date Sales This Year versus Period to Date Sales Last Year?

I am trying to solve for sales period to date this year compared to period to date last year, using a Period slicer.

 

(sample data included at end of post)

 

Slicer = 2021 Period 2

 

Desired Result:

Today is the first day 2021 Period 2 and sales are $100

First day of the 2020 Period 2 sales were $100

Then Period To Date sales change this year - last year = $0

 

Current Result (Bad!)

Today is the first day of the 2021 Period 2 and sales are $100

Total 2020 Period 2 sales last year last = $2,800

Sale change versus last year = -$2,700

 

The problem seems to be that my slicer wants to use all days from 2020 Period 2, when I only want it to use the first day of 2020 Period 2.

 

Stated another way, I want a DAX statement that says:

 

Give me this year sales Period to Date and last year sales Period to Date and calculate the change.

 

Sample data below.  Any help would be appreciated!

 

Business DateYearPeriodWeekDayYear_PeriodSales
1/28/202020202112020P2100
1/29/202020202122020P2100
1/30/202020202132020P2100
1/31/202020202142020P2100
2/1/202020202152020P2100
2/2/202020202162020P2100
2/3/202020202172020P2100
2/4/202020202282020P2100
2/5/202020202292020P2100
2/6/2020202022102020P2100
2/7/2020202022112020P2100
2/8/2020202022122020P2100
2/9/2020202022132020P2100
2/10/2020202022142020P2100
2/11/2020202023152020P2100
2/12/2020202023162020P2100
2/13/2020202023172020P2100
2/14/2020202023182020P2100
2/15/2020202023192020P2100
2/16/2020202023202020P2100
2/17/2020202023212020P2100
2/18/2020202024222020P2100
2/19/2020202024232020P2100
2/20/2020202024242020P2100
2/21/2020202024252020P2100
2/22/2020202024262020P2100
2/23/2020202024272020P2100
2/24/2020202024282020P2100
1/26/202120212112021P2100
1/27/202120212122021P2 
1/28/202120212132021P2 
1/29/202120212142021P2 
1/30/202120212152021P2 
1/31/202120212162021P2 
2/1/202120212172021P2 
2/2/202120212282021P2 
2/3/202120212292021P2 
2/4/2021202122102021P2 
2/5/2021202122112021P2 
2/6/2021202122122021P2 
2/7/2021202122132021P2 
2/8/2021202122142021P2 
2/9/2021202123152021P2 
2/10/2021202123162021P2 
2/11/2021202123172021P2 
2/12/2021202123182021P2 
2/13/2021202123192021P2 
2/14/2021202123202021P2 
2/15/2021202123212021P2 
2/16/2021202124222021P2 
2/17/2021202124232021P2 
2/18/2021202124242021P2 
2/19/2021202124252021P2 
2/20/2021202124262021P2 
2/21/2021202124272021P2 
2/22/2021202124282021P2 
1 ACCEPTED SOLUTION
edhans
Super User
Super User

This isn't the final solution, but will get you pointed in the right direction. You need to have a true date table. You have commingled a date dim (dimension) table and a fact table. I've added a date table to the model from here

 

The measure to get YTD sales is:

YTD Sales = 
VAR varCurrentDate = MAX('Date'[Date])
VAR varCurrentYear = MAX('Date'[Year])
VAR varCumulativeSales = 
    CALCULATE(
        SUM('Table'[Sales]),
        FILTER(
            ALL('Date'[Date],'Date'[Year]),
            'Date'[Year] = varCurrentYear
                && 'Date'[Date] <= varCurrentDate
        )
    )
RETURN
    varCumulativeSales

The measure to get prior YTD sales then is pretty much the same but rolled back 1 year.

PYTD Sales = 
VAR varCurrentDate = MAX('Date'[Date]) - 365
VAR varCurrentYear = MAX('Date'[Year]) - 1
VAR varCumulativeSales = 
    CALCULATE(
        SUM('Table'[Sales]),
        FILTER(
            ALL('Date'[Date],'Date'[Year]),
            'Date'[Year] = varCurrentYear
                && 'Date'[Date] <= varCurrentDate
        )
    )
RETURN
    varCumulativeSales

You can then add another measure that is just [YTD Sales] - [PYTD Sales]

 

Here is my file. This isn't perfect. You have a lot of missing data, but I get this is a sample. You'll note I hid values in the future using the IsFuture field of my date table. You may want to take a different approach if you don't have sales through today available.

 

Here is my PBIX file.



Did I answer your question? Mark my post as a solution!
Did my answers help arrive at a solution? Give it a kudos by clicking the Thumbs Up!

DAX is for Analysis. Power Query is for Data Modeling


Proud to be a Super User!

MCSA: BI Reporting

View solution in original post

3 REPLIES 3
edhans
Super User
Super User

This isn't the final solution, but will get you pointed in the right direction. You need to have a true date table. You have commingled a date dim (dimension) table and a fact table. I've added a date table to the model from here

 

The measure to get YTD sales is:

YTD Sales = 
VAR varCurrentDate = MAX('Date'[Date])
VAR varCurrentYear = MAX('Date'[Year])
VAR varCumulativeSales = 
    CALCULATE(
        SUM('Table'[Sales]),
        FILTER(
            ALL('Date'[Date],'Date'[Year]),
            'Date'[Year] = varCurrentYear
                && 'Date'[Date] <= varCurrentDate
        )
    )
RETURN
    varCumulativeSales

The measure to get prior YTD sales then is pretty much the same but rolled back 1 year.

PYTD Sales = 
VAR varCurrentDate = MAX('Date'[Date]) - 365
VAR varCurrentYear = MAX('Date'[Year]) - 1
VAR varCumulativeSales = 
    CALCULATE(
        SUM('Table'[Sales]),
        FILTER(
            ALL('Date'[Date],'Date'[Year]),
            'Date'[Year] = varCurrentYear
                && 'Date'[Date] <= varCurrentDate
        )
    )
RETURN
    varCumulativeSales

You can then add another measure that is just [YTD Sales] - [PYTD Sales]

 

Here is my file. This isn't perfect. You have a lot of missing data, but I get this is a sample. You'll note I hid values in the future using the IsFuture field of my date table. You may want to take a different approach if you don't have sales through today available.

 

Here is my PBIX file.



Did I answer your question? Mark my post as a solution!
Did my answers help arrive at a solution? Give it a kudos by clicking the Thumbs Up!

DAX is for Analysis. Power Query is for Data Modeling


Proud to be a Super User!

MCSA: BI Reporting

Thank you very much - and for being forgiving about incomplete data, including the date table (lesson learned).

 

I was able to recreate your measures and they did not error out.

 

Just so I understand, if I now put a slicer with year and date, these measures will calculate based upon the slicer selection?

 

Thanks again.

They should, yes. The slicer will filter those visuals which will in turn filter the data within it for the relevant date ranges.



Did I answer your question? Mark my post as a solution!
Did my answers help arrive at a solution? Give it a kudos by clicking the Thumbs Up!

DAX is for Analysis. Power Query is for Data Modeling


Proud to be a Super User!

MCSA: BI Reporting

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.