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
PeterStuhr
Helper V
Helper V

Need help creating YTD and LYTD and make Index as Card

Hi,

 

I am having problems making my LYTD work.

 

I have a SalesTable containing "Order Date" and "Revenue EUR".

 

I have created a Year To Date on "Revenue EUR" by the quick measure feature. I selected the "Order Date" as date.

This seem to work.

 

My problem

When creating "Last Year To Date", I have tried the following:

 

LYTD Sales = CALCULATE([SumOfSales]; DATEADD(SalesTable'[Order Start Date.Date];-1;YEAR))
Also tried:
LYTD Sales2 = CALCULATE([SumOfSales2];SAMEPERIODLASTYEAR('Sales (2)'[Order Start Date.date]))
 
Today is 1 May 2019. So my goal would be, that it sums 1 Jan 2018 - 1 May 2018.
 
What both of them does is that it sums 1 Jan 2018 to May 31 2018.
 
Why is that?
1 ACCEPTED SOLUTION
Anonymous
Not applicable

Need to be sure that your date table contains all dates, not just dates you have in your Sales Table.  Few ways to ensure that, but can create a calculated table with the following code ( just be sure to swap out the Orders [Order Date])

DimCalendar =
ADDCOLUMNS(
    CALENDAR(
        DATE( YEAR(MIN( Orders[Order Date])),1,1),
        DATE( YEAR( MAX ( Orders[Order Date])),12,31)),
        "Year", YEAR([Date]),
        "Month", MONTH( [Date] ),
        "MonthName", FORMAT( [Date], "mmmm"),
   )

Relate that your fact table and use the columns from DimCalendar for filters ( rows, columns, slicers, etc)

 

Then using the following measures, you can get YTD and LYTD sales. 

Total Sales = 
    SUM( 'FactOrder'[TotalSales] )

YTD Sales = 
    CALCULATE( 
        [Total Sales], 
        DATESYTD(DimCalendar[Date])
    )

LYTD Sales = 
    CALCULATE( 
        [YTD Sales], 
        SAMEPERIODLASTYEAR( DimCalendar[Date] )
    )

 which produces this table 

YTD and LYTD.png

View solution in original post

7 REPLIES 7
Anonymous
Not applicable

@PeterStuhr 
Do you have a dedicated Calendar table?  If so, is it marked as data table?

How to Mark as Date Table.pngThat's the first ( and really most important) step to ensure all the time-intelligence functions work correctly.

 

-N

Hi, thanks!

 

I have just created a table containing Dates and set it as Date table.

 

However, it does not seem to fix my issue.

Anonymous
Not applicable

Need to be sure that your date table contains all dates, not just dates you have in your Sales Table.  Few ways to ensure that, but can create a calculated table with the following code ( just be sure to swap out the Orders [Order Date])

DimCalendar =
ADDCOLUMNS(
    CALENDAR(
        DATE( YEAR(MIN( Orders[Order Date])),1,1),
        DATE( YEAR( MAX ( Orders[Order Date])),12,31)),
        "Year", YEAR([Date]),
        "Month", MONTH( [Date] ),
        "MonthName", FORMAT( [Date], "mmmm"),
   )

Relate that your fact table and use the columns from DimCalendar for filters ( rows, columns, slicers, etc)

 

Then using the following measures, you can get YTD and LYTD sales. 

Total Sales = 
    SUM( 'FactOrder'[TotalSales] )

YTD Sales = 
    CALCULATE( 
        [Total Sales], 
        DATESYTD(DimCalendar[Date])
    )

LYTD Sales = 
    CALCULATE( 
        [YTD Sales], 
        SAMEPERIODLASTYEAR( DimCalendar[Date] )
    )

 which produces this table 

YTD and LYTD.png

Thanks! I can see that it does what I want it to in the table.

 

However, what I really need is a YTD Index. I created it like this "Index test = ([YTD Sales]/[LYTD Sales])*100" and put it in the table.

Power BI Desktop.png

As you can see we are index 94 right now (May 1). That number, I would like to show always in a card. But when I put it in the card, it does not understand it. Is there a way I  can display this number in a card, without having the table?

Anonymous
Not applicable

No problem, would just have to add in a few lines of code to account for the date. I'm not sure on your data set and how often it refreshes, but do you want it show the last avialable date or do you want it based on today's date. So if there is no sales data for today, the card could say something like "No Data Availble" or something like that. 

Mariusz
Community Champion
Community Champion

Hi @PeterStuhr 

The below should do the trick, just need to adjust it slightly.

LYTD Sales = 
TOTALYTD(
    CALCULATE(
        [Sales], --Your Measure
        SAMEPERIODLASTYEAR('Calendar'[Date]) --date from your Date dimension
    ),
    'Calendar'[Date], --date from your Date dimension
    ALL('Calendar'[Date]) -- Date dimension table
)

Hope this helps 

Mariusz

Hi,

 

I have made the following, but it is blank:

 

LYTD Sales =
TOTALYTD(
CALCULATE(
[SumOfSales]; --Your Measure
SAMEPERIODLASTYEAR('date'[Date]) --date from your Date dimension
);
'date'[Date]; --date from your Date dimension
ALL('date'[Date]) -- Date dimension table
)

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.