【发布时间】:2021-10-21 15:16:20
【问题描述】:
我正在尝试在 DAX (PowerBI) 中创建度量,目前我正在使用 SWITCH 语句检查“关系”表的值,以找出与我的日历维度一起使用的关系。这一切都很好,见下文:-
My Measure = SWITCH(
// Which calendar relationship should we use?
MIN('Active Calendar Relationship'[Date Relationship]),
"Some Date",
CALCULATE(
DISTINCTCOUNT('Measures'[SomeField]),
USERELATIONSHIP(
'Calendar'[Key],
'SomeFactTable'[SomeDateKey]
)
),
"Other Date",
CALCULATE(
DISTINCTCOUNT('Measures'[SomeField]),
USERELATIONSHIP(
'Calendar'[Key],
'SomeFactTable'[SomeDateKey]
)
),
// Default to 'Some Date'
CALCULATE(
DISTINCTCOUNT('Measures'[SomeField]),
USERELATIONSHIP(
'Calendar'[Key],
'SomeFactTable'[SomeDateKey]
)
)
)
我现在的问题是我还有一个“一天中的时间”维度,所以我需要使用我在另一个表中的选择时间,您可以在其中选择所需的一天中的时间关系.问题在于将上面的代码扩展为使用多个“USERELATIONSHIP”语句,但由于您只能在“CALCULATE”中使用“USERELATIONSHIP”,我唯一的选择是以某种方式获取所选日期/时间值的每个可能组合并构建一个巨大的 SWITCH 语句。下面有点像这样(希望你能看到它有多大):-
Some Measure = SWITCH(
// Which 'calendar' relationship should we use?
MIN('Active Calendar Relationship'[Date Relationship]),
"Some Date",
SWITCH(
// Which 'time' relationship should we use?
MIN('Active Time Relationship'[Time Relationship]),
"Some Time",
CALCULATE(
DISTINCTCOUNT('Measures'[SomeField]),
USERELATIONSHIP(
'Time'[Key],
'Measures'[SomeTimeKey]
),
USERELATIONSHIP(
'Calendar'[Key],
'Measures'[SomeDateKey]
)
),
"Other Time",
CALCULATE(
DISTINCTCOUNT('Measures'[SomeField]),
USERELATIONSHIP(
'Time'[Key],
'Measures'[OtherTimeKey]
),
USERELATIONSHIP(
'Calendar'[Key],
'Measures'[SomeDateKey]
)
),
// Default to 'Some Date' and 'Some Time'
CALCULATE(
DISTINCTCOUNT('Measures'[SomeField]),
USERELATIONSHIP(
'Time'[Key],
'Measures'[SomeTimeKey]
),
USERELATIONSHIP(
'Calendar'[Key],
'Measures'[SomeDateKey]
)
)
),
"Other Date",
SWITCH(
// Which 'time' relationship should we use?
MIN('Active Time Relationship'[Time Relationship]),
"Some Time",
CALCULATE(
DISTINCTCOUNT('Measures'[SomeField]),
USERELATIONSHIP(
'Time'[Key],
'Measures'[SomeTimeKey]
),
USERELATIONSHIP(
'Calendar'[Key],
'Measures'[OtherDateKey]
)
),
"Other Time",
CALCULATE(
DISTINCTCOUNT('Measures'[SomeField]),
USERELATIONSHIP(
'Time'[Key],
'Measures'[OtherTimeKey]
),
USERELATIONSHIP(
'Calendar'[Key],
'Measures'[OtherDateKey]
)
),
// Default to 'Other Date' and 'Some Time'
CALCULATE(
DISTINCTCOUNT('Measures'[SomeField]),
USERELATIONSHIP(
'Time'[Key],
'Measures'[SomeTimeKey]
),
USERELATIONSHIP(
'Calendar'[Key],
'Measures'[OtherDateKey]
)
)
)
and so on...
)
我认为可以减少代码的唯一另一种方法是将所有组合创建为度量,但是会有很多度量,所以它几乎就像“卸载”问题一样。我不是 DAX 专家,但我进行了搜索,我正在努力寻找任何其他/更好的解决方案来解决这个问题。希望有另一种比上述更有效的方法......
帮助 :-)
【问题讨论】: