Forum Discussion
Creating a Measure based on Previous day
Hello all!
I have a Sales table of employees by day by item category and total time on sale
I want to create a measure that shows the averge time for sales for by employee for the previous day based on current day.
Example
| Date | Employee | Group | Item group | Sales | Time |
| 5/19/2020 | Joe | Tech | IOS | 3 | 819 |
| 5/19/2020 | Joe | Tech | Android | 11 | 2247 |
| 5/19/2020 | Joe | Tech | Windows | 31 | 11056 |
| 5/19/2020 | Joe | Tech | Gaming | 1 | 176 |
| 5/18/2020 | Joe | Tech | IOS | 4 | 919 |
| 5/18/2020 | Joe | Tech | Android | 15 | 254 |
| 5/18/2020 | Joe | Tech | Windows | 26 | 12001 |
| 5/18/2020 | Joe | Tech | Gaming | 14 | 201 |
I want to find out the average time for Joe on 5/18 so the formula is sum(Time)/sum(sales)
I've been using the following formula
Calculate(divide(sum(Time),sum(Sales),0),filter(date = now()-1)) and it returns nothing.
Other times its gives me the "The expression refers to multiple columns. Multiple columns cannot be converted to a scalar value" or the Divide by Zero error.
I'm stuck here. Any help would be greatly appreciated.
- Anonymous6 years ago
You should use Today() instead of Now(), because Now()-1 return current time-1 second, not previous day. And for the sample you provided, you can test it by replace Today() with Date(2020,5,19) to check the average of 5/18.
Measure Calculate(divide(sum([Time ]),sum([Sales]),0),filter('Table',[Date]= TODAY()-1))Paul Zheng _ Community Support Team
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
7 Replies
- parry2kSuper User
nosaj03 As a best practice, add date dimension in your model and use it for and time intelligence calculations. There are many posts on how to add date dimension and below is the link to a few. Once the date dimension is added, mark it as a date table on table tools.
https://www.sqlbi.com/articles/creating-a-simple-date-table-in-dax/
https://radacad.com/create-a-date-dimension-in-power-bi-in-4-steps-step-1-calendar-columnsand then you can use something like this
Previous Day SAles = CALCULATE ( SUM ( Table[SAles] ), DATEADD ( CalendarTable[Date], -1, DAY ) )I would ❤ Kudos if my solution helped. 👉 If you can spend time posting the question, you can also make efforts to give Kudos whoever helped to solve your problem. It is a token of appreciation!
- amitchandakSuper User
nosaj03 , To avoid divide by 0 use divide . which you have done correctly
Calculate(divide(sum(Time),sum(Sales),0),filter(Table,[date] = Today()-1))
This might not work if date is selected then you have to use all
Calculate(divide(sum(Time),sum(Sales),0),filter(all(Table),[date] = Today()-1))Better to use a date table
Last Day Non Continous = CALCULATE(divide(sum(Time),sum(Sales),0),filter(all('Date'),'Date'[Date] =MAXX(FILTER(all('Date'),'Date'[Date]<max('Date'[Date])),Table['Date'])))
Day behind Sales = CALCULATE(divide(sum(Time),sum(Sales),0),dateadd('Date'[Date],-1,Day))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.- nosaj03Helper II
amitchandak thank you for the insight. I have a date table that I created.
When I use the formula you provided below
Day behind Sales = CALCULATE(divide(sum(Time),sum(Sales),0),dateadd('Date'[Date],-1,Day))
It is still giving me the total based on the entire table and not the previous day.
- amitchandakSuper User
nosaj03 , can you share the screenshot of the issue. This should have worked. Hope you are using date and any other date-related column from date table
- AnonymousNot applicable
You should use Today() instead of Now(), because Now()-1 return current time-1 second, not previous day. And for the sample you provided, you can test it by replace Today() with Date(2020,5,19) to check the average of 5/18.
Measure Calculate(divide(sum([Time ]),sum([Sales]),0),filter('Table',[Date]= TODAY()-1))Paul Zheng _ Community Support Team
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.- nosaj03Helper II
Anonymous Thanks! This worked out perfectly!!!!