在 DAX 中测量以仅针对 Power BI 计算所选月份的 YTD [英] Measure in DAX to calculate YTD for chosen month only for Power BI

查看:61
本文介绍了在 DAX 中测量以仅针对 Power BI 计算所选月份的 YTD的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何构建 DAX 度量来计算特定月份的 YTD 值的总和?

这里我们有按月分组的 FactTable.FactTable 填充了实际数据和预测数据.了解实际结束时间的唯一方法是表 [截止日期] 列 [YTD 结束] 中的信息.在 [End of YTD] 列中的表 [Cut of date] 中——它是一个单值表——我们选择了有趣的月份,我们希望查看 YTD 的计算.在我们的例子中,它是三月.FactTable 每月不定期更新,通常延迟一个月.由于不定期更新,无法将其链接到像 TODAY 这样的时间函数.

我们希望在 [YTD 结束] 月份的黄牌视觉中显示正确的 YTD 值.当我们点击2018-03"上的切片器时,我们几乎得到了我们想要的——黄牌中的正确值 66.然而,这个解决方案不是自动的.我希望在 [YTD 结束] 月份发生变化时自动查看正确的值,在我们的例子中是四月或五月.我不希望它由用户完成.

可以从以下文件下载我拼命的努力:

我建议您更改度量的计算以避免在某些情况下出现缺失值:

总计 = SUM(FactTable[Value])MTD = TOTALMTD([总计], '日历'[日期])YTD = TOTALYTD([Total], '日历'[Date])

<小时>

更新:

对我来说,您现在想要实现的目标更加清晰,但对我来说,这仍然是一个

对于 YTD 结束 视觉效果,您可以使用

How to construct DAX measure to calculate sum of YTD value for specific month?

Here we have FactTable grouped by months. FactTable is filled with both Actual data and Forecast data. The only way to know when Actual end is information in table [Cut of date] in column [End of YTD]. In table [Cut of date] in column [End of YTD] – it is a single value table – we have the interesting chosen month, for which we want to see the calculation of YTD. In our case it is March. FactTable is updated irregularly every month with usually one month delay. There is no way of linking it to time functions like TODAY because of irregular update.

We would like to have a correct value of YTD displayed in yellow Card Visual for the month [End of YTD]. When we click on the slicer on "2018-03" we get almost what we want – correct value of 66 in the yellow Card. However this solution is not automatic. I want to see correct value automatically when the [End of YTD] month changes, in our case to April or then to May. I do not want it done by user.

My desperate effort can be downloaded from file: DAX YTD.pbix

I pursued the deer in various ways:

  1. By using FILTER function in DAX measures. But it seems that the FILTER function is to harsh. It is applied to fact table first, selecting only one month, and then calculating YTD value wrongly. So if there would be any option for forcing order of calculation and filtering, there would be hope.
  2. I tried SWITCH function to display proper result for specific month and 0 or null for other months. Although I succeed in this, I was not able to take advantage of it. When it came to filtering I was as hopeless as before. BTW I would be able to make it if SWITCH produced totals at the end of the table, but it does not. Surprisingly.
  3. I put some hopes in RELATED function to display proper results in the [Cut off date] table. I have not walk out of the fog so far.

I would appreciate your help.

Update before bounty. Going to higher level. I have introduced a Category column to FactTable. Please download DAX YTD by category.pbix. So filtering gets more complex now. I would like to have correct YTD figures for Apples category.

解决方案

Did you use the Date column from the Calendar table, instead of the one from FactTable?

If you use the date column from FactTable, when you apply a filter on the date, it will filter on the fact records which is in March, and then do the calculation afterwards, hence the result 33.

If you use the one from Calendar, when you apply a filter on it, it filters the records on Calendar (which you want to show in the chart), so the underlying calculation will still remain intact.


A working example:

Calendar = CALENDAR(DATE(2010, 1, 1), DATE(2020, 12, 31))

I suggest you to change the calculations of the measures to avoid missing values in some cases:

Total = SUM(FactTable[Value])
MTD = TOTALMTD([Total], 'Calendar'[Date])
YTD = TOTALYTD([Total], 'Calendar'[Date])


UPDATE:

It's much clearer to me what you want to achieve now but it still seems an XY problem to me.

I understand that you want to show the dashboard as is so that users do not need to click/input every time to see what they are supposed to see. That's why I don't get why you need to create a new table to store the Cut off date (End of YTD). How is it going to be maintained automatically?

The relative date filtering solution above actually still works in the .pbix file you've shared. If you drag the Date column from the Calendar table to visual level filters for the yellow card and add the relative date filtering, it should work as below:

For the End of YTD visual, you can use the following measure to get the first day of last calendar month, so you don't need to create another table for it:

End of YTD = EOMONTH(TODAY(), -2) + 1

And hopefully this is what you want to achieve:

Updated file for your reference.


UPDATE again:

I think you'll have to write your own YTD calculation instead of using the built-in one, so that you can make use of the cut off date you defined in another table. Here I assume that you have one and only one row in 'Cut off date'[End of YTD]. Note that I've added ALL() to the filter, so that the yellow card remains the same (66) instead of showing blank when some other rows/filters are clicked:

YTD_Special = 
CALCULATE(
    [Total],
    FILTER(
        ALL(FactTable),
        FactTable[Date] >= DATE(YEAR(VALUES('Cut off date'[End of YTD])), 1, 1) &&
        FactTable[Date] <= VALUES('Cut off date'[End of YTD])
    )
)

这篇关于在 DAX 中测量以仅针对 Power BI 计算所选月份的 YTD的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆