[英]How to calculate total sales as of first day of the current month as Previous month sales in power BI
I have salesamount column and the saledate column in my table.我的表中有 salesamount 列和 saledate 列。 so I need to calculate total sales for each month based on below calculation.所以我需要根据以下计算计算每个月的总销售额。
total sales for current month=sum(salesamount) as of 1st day of the next month本月总销售额=sum(salesamount) 截至下月1日
for example sales of December-2021 should be calculated based on the total sales as on Jan-1-2022.例如,2021 年 12 月的销售额应根据 2022 年 1 月 1 日的总销售额计算。 total sales for January should be blank until Feb-1-2022 as it should be the total sales as on feb-1-2022.一月份的总销售额在 2022 年 2 月 1 日之前应该是空白的,因为它应该是 2022 年 2 月 1 日的总销售额。 I am very much confused how we can achieve this in Dax.我很困惑我们如何在 Dax 中实现这一点。 Really appreciate your help.非常感谢您的帮助。
If I understand your question correctly, you can use the following DAX measure:如果我正确理解您的问题,您可以使用以下 DAX 度量:
Total Sales =
var currentDate = MAX(myTable[saleDate])
var firstOfMonth = DATE(YEAR(CALCULATE(MAX(myTable[saledate]), ALL(myTable))),
MONTH(CALCULATE(MAX(myTable[saledate]), ALL(myTable))), 1)
var result = SUM(myTable[salesamount])
Return IF(currentDate < firstOfMonth, result)
This will take the current date of the report context and compare it to the 1st of the current month.这将获取报告上下文的当前日期并将其与当前月份的 1 日进行比较。 If the currentDate is less than the 1st of the current month, the result will be calculated.如果 currentDate 小于当月 1 日,则计算结果。 If your dataset has a Date table, you can replace the 'myTable[saledate]' references with a reference to the Date column in your date table.如果您的数据集有一个日期表,您可以将“myTable[saledate]”引用替换为对日期表中日期列的引用。
With some sample data, here are the results:使用一些示例数据,结果如下:
(I added the firstOfMonth column for demonstration purposes) (出于演示目的,我添加了 firstOfMonth 列)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.