1)出于本文的目的,我使用一个非常简单的模型-一个Sales表(仅包含Date和Sales)和一个Date表。
2)创建如下所示的3个度量,然后将这3个度量与一个月切片器一起添加到报告中,如下所示。您可以在切片器中更改月份,并验证所选月份的度量值是否已更改。
Sales (Selected Month) = SUM ( Sales[Sales] ) Sales Last Year = CALCULATE ( SUM ( Sales[Sales] ), SAMEPERIODLASTYEAR ( ‘Date'[Date] ) ) Sales YTD = TOTALYTD ( SUM ( Sales[Sales] ), ‘Date'[Date] )
3)下一步是制定一个显示最近N个月的度量。让我们创建一个称为N 的What If参数,其值从1到24,增量为1。将其放置在图表中,如下所示
4)此技术的主要步骤是–创建一个度量标准,以显示最近N个月的销售总额。重要的是要知道将“日期”表中的“月份”放不下,所以我们要做的是在“销售”表中创建“月份”列,然后将其用作条形图的轴。在Sales表中创建2个计算列(MonthYear以及MonthYearNo(用于对MonthYear列进行排序)),以及度量-Sales(最近n个月)。
MonthYear = RELATED ( ‘Date'[MonthofYear] ) MonthYearNo = RELATED ( ‘Date'[MonthYearNo] ) Sales (last n months) = VAR MaxFactDate = CALCULATE ( MAX ( Sales[Date] ), ALL ( ‘Date’ ) ) — ignore the selected date filter, and find the max of date in Sales table VAR FDate = ENDOFMONTH ( ‘Date'[Date] ) — get the last day of the month selected in the date filter VAR Edate = EDATE ( FDate, – [N Value] ) — get the last day of -N months RETURN IF ( MaxFactDate <= MAX ( ‘Date'[Date] ) && MaxFactDate > Edate, CALCULATE ( SUM ( Sales[Sales] ), ALL ( ‘Date’ ) ) ) — if the date in the fact table is between the last N months, display Sales, else nothing. Note that we are ignoring the date filter, only respect the date in Fact
或者
Sales (last n months) = CALCULATE ( SUM ( Sales[Sales] ), DATESINPERIOD ( ‘Date'[Date], MAX ( ‘Date'[Date] ), – [N Value], MONTH ) )
5)现在,创建一个条形图,在“轴”上的“年年”和在值上的“销售额”(最近n个月),如下所示。