Forum Discussion
Earlier Function
Hi,
I have the following table
| Product | Date | Qty | Previous Year Results |
| AAA | 1/1/2017 | 5 | |
| BBB | 1/1/2017 | 3 | |
| CCC | 1/1/2017 | 3 | |
| AAA | 1/3/2017 | 8 | |
| BBB | 1/3/2017 | 4 | |
| CCC | 1/3/2017 | 6 | |
| CCC | 1/9/2017 | 7 | |
| AAA | 1/1/2018 | 6 | |
| CCC | 1/1/2018 | 7 | |
| BBB | 1/3/2018 | 7 | |
| CCC | 1/9/2018 | 8 | |
| AAA | 1/1/2019 | 9 | |
| BBB | 1/3/2019 | 6 | |
| CCC | 1/9/2019 | 7 |
in "Previous Year Results" column, I wanted to calculate the sold qty for the same day in the previous year AND for the same product
When I type the following formula, it filters the dates
CACLCULATE(SUM(TABLE[QTY]),FILTER(TABLE,DATEADD(TABLE[DATE].[Date],1,YEAR)=EARLIER(TABLE[DATE].[Date])))
it works and sums the qty sold in the same date in previous year (regardless the product)
but When I add another filter with earlier value in Product type it gives me blank.
I need help
how exactly do you filter by product? show please full wrong sentence.
in the most common case it can look like
CACLCULATE(SUM(TABLE[QTY]), FILTER(TABLE, DATEADD(TABLE[DATE].[Date],1,YEAR)=EARLIER(TABLE[DATE].[Date]) && TABLE[Product] = EARLIER(TABLE[Product]) ) )amaniramahi , Try a new column like
sumx(filter(Table,Table[Product] =earlier(Table[Product]) && year(Table[Date]) = year(earlier(Table[Date]))-1),TABLE[QTY])
Measure
example with date table
YTD Sales = CALCULATE(SUM(Sales[Sales Amount]),DATESYTD('Date'[Date],"12/31")) Last YTD Sales = CALCULATE(SUM(Sales[Sales Amount]),DATESYTD(dateadd('Date'[Date],-1,Year),"12/31"))refer blog -https://medium.com/@amitchandak.1978/power-bi-ytd-questions-time-intelligence-1-5-e3174b39f38a
To get the best of the time intelligence function. Make sure you have a date calendar and it has been marked as the date in model view. Also, join it with the date column of your fact/s. Refer :
https://radacad.com/creating-calendar-table-in-power-bi-using-dax-functions
https://www.archerpoint.com/blog/Posts/creating-date-table-power-bi
https://www.sqlbi.com/articles/creating-a-simple-date-table-in-dax/
See if my webinar on Time Intelligence can help: https://community.powerbi.com/t5/Webinars-and-Video-Gallery/PowerBI-Time-Intelligence-Calendar-WTD-YTD-LYTD-Week-Over-Week/m-p/1051626#M184
Appreciate your Kudos.
6 Replies
- az38Community Champion
how exactly do you filter by product? show please full wrong sentence.
in the most common case it can look like
CACLCULATE(SUM(TABLE[QTY]), FILTER(TABLE, DATEADD(TABLE[DATE].[Date],1,YEAR)=EARLIER(TABLE[DATE].[Date]) && TABLE[Product] = EARLIER(TABLE[Product]) ) )- amaniramahiHelper V
Hi az38 , I tried your formula, it gives me blank for all column 😞
- az38Community Champion
maybe you need to clear all your context with ALL() function, like
CALCULATE(SUM(TABLE[QTY]), FILTER(ALL(TABLE), DATEADD(TABLE[DATE].[Date],1,YEAR)=EARLIER(TABLE[DATE].[Date]) && TABLE[Product] = EARLIER(TABLE[Product]) ) )otherwise, you have to check your data or provide us data example
- amitchandakSuper User
amaniramahi , Try a new column like
sumx(filter(Table,Table[Product] =earlier(Table[Product]) && year(Table[Date]) = year(earlier(Table[Date]))-1),TABLE[QTY])
Measure
example with date table
YTD Sales = CALCULATE(SUM(Sales[Sales Amount]),DATESYTD('Date'[Date],"12/31")) Last YTD Sales = CALCULATE(SUM(Sales[Sales Amount]),DATESYTD(dateadd('Date'[Date],-1,Year),"12/31"))refer blog -https://medium.com/@amitchandak.1978/power-bi-ytd-questions-time-intelligence-1-5-e3174b39f38a
To get the best of the time intelligence function. Make sure you have a date calendar and it has been marked as the date in model view. Also, join it with the date column of your fact/s. Refer :
https://radacad.com/creating-calendar-table-in-power-bi-using-dax-functions
https://www.archerpoint.com/blog/Posts/creating-date-table-power-bi
https://www.sqlbi.com/articles/creating-a-simple-date-table-in-dax/
See if my webinar on Time Intelligence can help: https://community.powerbi.com/t5/Webinars-and-Video-Gallery/PowerBI-Time-Intelligence-Calendar-WTD-YTD-LYTD-Week-Over-Week/m-p/1051626#M184
Appreciate your Kudos.- amaniramahiHelper V
amitchandak why should I filter only on YEAR? there are many dates within the same year 😞
- amaniramahiHelper V
Thank you both, I worked for both solutions