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
Anonymous
Not applicable

Comparing last year values

Hi

I've a transactional table that contains the sales done for each day.

What I'm trying to achieve is to compare sales by each sales person as follows:

1) YTD sales this year VS last year. So, assume we are now in Oct. So, I want to compare YTD Sales till Oct 2021 compared to YTD sales last year (Jan till Oct 2020).

2) Monthly sales this year compared to last year. E.g. Jan 2020 VS Jan 2021, Feb 2020 VS Feb 2021

3) I want it to be dynamic, so let's stay we start 2022. Then, my current fiscal year will be 2022 and I want to compare in this case YTD Jan 2022 to Jan 2021.

 

 

Sampe data below:

DateSales PersonItem NumberAmount ($)QTY
01-Jan-20Sales A1001 $    200.003
01-Jan-20Sales B1002 $    100.004
02-Feb-20Sales C1003 $    300.005
02-Feb-20Sales A1004 $    500.004
03-Mar-20Sales B1005 $ 1,000.007
03-Mar-20Sales C2001 $    900.008
04-Apr-20Sales A2002 $    950.002
04-Apr-20Sales B2003 $    302.004
01-May-20Sales C2004 $    311.001
01-May-20Sales A2005 $    400.009
02-Jun-20Sales B3001 $    100.005
02-Jun-20Sales C3002 $    200.006
03-Jul-20Sales A3003 $    300.003
03-Jul-20Sales B3004 $    800.004
04-Aug-20Sales C3005 $    900.005
04-Aug-20Sales A4001 $    400.008
06-Sep-20Sales B4002 $    500.002
06-Sep-20Sales C4003 $    600.004
01-Oct-20Sales A4004 $    300.008
01-Oct-20Sales B4005 $    400.009
02-Nov-20Sales C5001 $    100.004
02-Nov-20Sales A5002 $    950.002
03-Dec-20Sales B5003 $    750.006
03-Dec-20Sales C5004 $    650.005
01-Jan-21Sales C1001 $    300.001
02-Feb-21Sales A1002 $    350.001
03-Mar-21Sales B1003 $    450.003
04-Apr-21Sales C1004 $    650.002
01-May-21Sales A1005 $    300.004
02-Jun-21Sales B2001 $    700.005
03-Jul-21Sales C2002 $    850.006
04-Aug-21Sales A2003 $    830.007
06-Sep-21Sales B2004 $    100.009
01-Oct-21Sales C2005 $    150.002

 

 

Thanks in Advance

1 ACCEPTED SOLUTION
TheoC
Super User
Super User

Hi @Anonymous 

 

You can achieve this by creating the following measures:

 

1. SalesMTD

 

SalesMTD = CALCULATE ( SUM ( 'Table'[Amount] ) , DATESMTD ( 'Date'[Date] ) )

2. SalesYTD

 

SalesYTD = CALCULATE ( SUM ( 'Table'[Amount] ) , DATESYTD ( 'Date'[Date] ) )

3. SalesLstYrMTD

 

SalesLstYrMTD = CALCULATE ( SUM ( 'Table'[Amount] ) , DATEADD ( 'Date'[Date] , -1 , MONTH ) )

 

4. SalesLstYTD

 

SalesLstYTD = CALCULATE ( SUM ( 'Table'[Amount] ) , DATEADD ( 'Date'[Date] , -1 , YEAR ) )

 

If you haven't already created a Date table, I strongly recommend you create one.  Link the 'Date'[Date] field to your 'Table'[Date] field that will automatically generate a one to many relationship.

 

Date =

VAR MinYear = YEAR ( MIN ( 'Table'[Date] ) )
VAR MaxYear = YEAR ( MAX ( 'Table'[Date] ) )

RETURN

ADDCOLUMNS (
    FILTER (
        CALENDARAUTO( ),
        AND ( YEAR ( [Date] ) >= MinYear, YEAR ( [Date] ) <= MaxYear )
    ),
    "Calendar Year", "CY " & YEAR ( [Date] ),
    "Month Name", FORMAT ( [Date], "mmmm" ),
    "Month Number", MONTH ( [Date] )
)
https://www.sqlbi.com/articles/creating-a-simple-date-table-in-dax/

 

Let me know if you need further assistance!

 

Hope this helps 🙂 

If I have posted a response that resolves your question, please accept it as a solution to formally close the post.

Also, if you are as passionate about Power BI, DAX and data as I am, please feel free to reach out if you have any questions, queries, or if you simply want to connect and talk to another data geek!

Want to connect?www.linkedin.com/in/theoconias

View solution in original post

9 REPLIES 9
TheoC
Super User
Super User

Hi @Anonymous 

 

You can achieve this by creating the following measures:

 

1. SalesMTD

 

SalesMTD = CALCULATE ( SUM ( 'Table'[Amount] ) , DATESMTD ( 'Date'[Date] ) )

2. SalesYTD

 

SalesYTD = CALCULATE ( SUM ( 'Table'[Amount] ) , DATESYTD ( 'Date'[Date] ) )

3. SalesLstYrMTD

 

SalesLstYrMTD = CALCULATE ( SUM ( 'Table'[Amount] ) , DATEADD ( 'Date'[Date] , -1 , MONTH ) )

 

4. SalesLstYTD

 

SalesLstYTD = CALCULATE ( SUM ( 'Table'[Amount] ) , DATEADD ( 'Date'[Date] , -1 , YEAR ) )

 

If you haven't already created a Date table, I strongly recommend you create one.  Link the 'Date'[Date] field to your 'Table'[Date] field that will automatically generate a one to many relationship.

 

Date =

VAR MinYear = YEAR ( MIN ( 'Table'[Date] ) )
VAR MaxYear = YEAR ( MAX ( 'Table'[Date] ) )

RETURN

ADDCOLUMNS (
    FILTER (
        CALENDARAUTO( ),
        AND ( YEAR ( [Date] ) >= MinYear, YEAR ( [Date] ) <= MaxYear )
    ),
    "Calendar Year", "CY " & YEAR ( [Date] ),
    "Month Name", FORMAT ( [Date], "mmmm" ),
    "Month Number", MONTH ( [Date] )
)
https://www.sqlbi.com/articles/creating-a-simple-date-table-in-dax/

 

Let me know if you need further assistance!

 

Hope this helps 🙂 

If I have posted a response that resolves your question, please accept it as a solution to formally close the post.

Also, if you are as passionate about Power BI, DAX and data as I am, please feel free to reach out if you have any questions, queries, or if you simply want to connect and talk to another data geek!

Want to connect?www.linkedin.com/in/theoconias

Anonymous
Not applicable

Thanks a lot @TheoC 

Qustion. The fiscal year for me starts on 1st Oct and Ends in 30 Sep. How can I use the above in this case? Is there a way to setup start of fiscal year?

@Anonymous absolutely.  You can adjust the SalesYTD to the following:

 

SalesYTD = CALCULATE ( SUM ( 'Table'[Amount] ) , DATESYTD ( 'Date'[Date] , "09-30" ) )

 

and replicate the same for the others 🙂

If I have posted a response that resolves your question, please accept it as a solution to formally close the post.

Also, if you are as passionate about Power BI, DAX and data as I am, please feel free to reach out if you have any questions, queries, or if you simply want to connect and talk to another data geek!

Want to connect?www.linkedin.com/in/theoconias

Anonymous
Not applicable

BTW @TheoC , does the above work with DATESMTD ? It gives me syntax error.

 

Also, how can I use this for the following DAX

SalesLstYrMTD = CALCULATE ( SUM ( 'Table'[Amount] ) , DATEADD ( 'Date'[Date] , -1 , MONTH ) )
Anonymous
Not applicable

You are Awesome @TheoC 

Just I noticed the following on your solution:

 

The following formula is not working, it doesn't accept to add the end date.

SalesMTD = CALCULATE ( SUM ( 'Table'[Amount] ) , DATESMTD ( 'Date'[Date] , "09-30" ) )

 How can I incorporate Fiscal Year end in this formula?

 

SalesLstYrMTD = CALCULATE ( SUM ( 'Table'[Amount] ) , DATEADD ( 'Date'[Date] , -1 , MONTH ) )

 

Thanks

Hi @Anonymous 

 

A pleasure my friend.

 

Okay, for the SalesMTD, you won't need to worry about the Fiscal Year End because it only sums to the MTD.  The same will be applicable for the SalesLstYrMTD so you don't need to worry about the year end period.

 

Let me know if that makes sense?

 

Thanks heaps,

Theo

If I have posted a response that resolves your question, please accept it as a solution to formally close the post.

Also, if you are as passionate about Power BI, DAX and data as I am, please feel free to reach out if you have any questions, queries, or if you simply want to connect and talk to another data geek!

Want to connect?www.linkedin.com/in/theoconias

Anonymous
Not applicable

Hi @TheoC  it makes sense but not working for some reason 😞

Here is what I'm facing.

For this measure:

SalesYTD = CALCULATE ( SUM ( 'Table'[Amount] ) , DATESYTD ( 'Date'[Date] , "09-30" ) )

If I use the date table that I created per your advise in the above measure, I get blank when I use the "09-30". BUT if I use the date that is in the original table "Table[Date]", then I get results. But I need to define the end of fiscal year for my results to be right.

 

So, if I can solve this, then I think all will work fine.

 

@Anonymous Apologies, can you chant "09-30" to "9/30" apologies!

If I have posted a response that resolves your question, please accept it as a solution to formally close the post.

Also, if you are as passionate about Power BI, DAX and data as I am, please feel free to reach out if you have any questions, queries, or if you simply want to connect and talk to another data geek!

Want to connect?www.linkedin.com/in/theoconias

Anonymous
Not applicable

Nop, not solving the problem.

 

So here's the behaviour.

1) When I put this measure for YTD in this format:

SalesYTD = CALCULATE ( SUM ( 'Table'[Amount] ) , DATESYTD ( 'Date'[Date] , "09-30" ) )

I get 0 as a result.

 

2) When I put this measure for YTD in this format (I use date in transaction table):

SalesYTD = CALCULATE ( SUM ( 'Table'[Amount] ) , DATESYTD ( 'Table'[Date] , "09-30" ) )

 I get results correct.

 

Now for last year YTD:

When I use this measure

SalesLstYrMTD = CALCULATE ( SUM ( 'Table'[Amount] ) , DATEADD ( 'Date'[Date] , -1 , MONTH ) )

 

I get values, but the sum is wrong. I think it's unaware of the Start and end of quarter. This is why the result is wrong.

 

Just sharing my thoughts.

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.