有没有办法让用户在 Power BI 的 dax groupby 公式中选择列?
Is there a way to let the user choose the columns in a dax groupby formula in Power BI?
我有一份 table 每周支出信息、性别和年龄段。
Sample Table = {
(1,"Man","0-10",9),
(1,"Woman","0-10",10),
(1,"Man","10-30",8),
(1,"Woman","10-30",2),
(1,"Man","30-60",4),
(1,"Woman","30-60",6),
(1,"Man","60+",9),
(1,"Woman","60+",8),
(2,"Man","0-10",6),
(2,"Woman","0-10",4),
(2,"Man","10-30",8),
(2,"Woman","10-30",8),
(2,"Man","30-60",7),
(2,"Woman","30-60",10),
(2,"Man","60+",3),
(2,"Woman","60+",8),
(3,"Man","0-10",3),
(3,"Woman","0-10",10),
(3,"Man","10-30",3),
(3,"Woman","10-30",10),
(3,"Man","30-60",1),
(3,"Woman","30-60",9),
(3,"Man","60+",8),
(3,"Woman","60+",5)
}
使用这个table,我可以计算出每组的消费金额。 'group' 可以用不同的方式定义:'Men'(仅性别)或 'Men in age group <60'(性别 + 年龄组)或 'Men in age group <60 in week 25'(性别 + 年龄组 + 周数)等。为了计算每组花费的金额(在这种情况下,按性别+周数分组),我使用以下 dax 代码创建了一个新的 table:
grouped spending =
VAR spending =
GROUPBY('Sample Table',
'Sample Table'[Weeknumber],
'Sample Table'[Gender],
"sum_spent_group",
SUMX(CURRENTGROUP(), 'Sample Table'[spent]))
RETURN
spending
并向此 table 添加一列以计算所选组花费的部分占总花费的金额:
spending part =
DIVIDE(CALCULATE(SUM('grouped spending'[sum_spent_group]),
ALLEXCEPT('grouped spending','grouped spending'[group])),
SUM('grouped spending'[sum_spent_group])
)
现在我可以显示一个组的支出占总支出的百分比:
有没有办法让用户选择(使用 buttons/bookmarks/etc..?)选择哪些列应该在 groupby 中?换一种说法;是否可以使此代码更通用,并让用户指定组组合?以及分子作为分母。
我意识到一种解决方案是为所有组组合制作列,但在我的真实数据中,我有更多的类别,所以这将花费很多 space(以及很多工作.. ).
非常感谢!
如果我没理解错的话,您实际上不需要为每个组创建单独的 table。
- 创建一个度量来计算将受切片器(您的组)影响的支出总和。类似于
spent_calc = SUM('Sample Table'[spent])
- 创建一个度量来计算总支出,而不管选择的切片器如何
CALCULATE(SUM('Sample Table'[spent]),ALL('Sample Table'))
- 创建计算总花费百分比的度量值
%_spent_by_selected_group = DIVIDE([spent_calc],[total_spent])
- 创建切片器并将百分比度量放入所需的视觉对象中,例如
现在用户可以创建属性的任意组合并查看花费总额的百分比。需要注意的是,如果你有很多类别,你当然必须有很多切片器,所以它可能不是最优雅的解决方案。不过可能是最简单的一个。
我有一份 table 每周支出信息、性别和年龄段。
Sample Table = {
(1,"Man","0-10",9),
(1,"Woman","0-10",10),
(1,"Man","10-30",8),
(1,"Woman","10-30",2),
(1,"Man","30-60",4),
(1,"Woman","30-60",6),
(1,"Man","60+",9),
(1,"Woman","60+",8),
(2,"Man","0-10",6),
(2,"Woman","0-10",4),
(2,"Man","10-30",8),
(2,"Woman","10-30",8),
(2,"Man","30-60",7),
(2,"Woman","30-60",10),
(2,"Man","60+",3),
(2,"Woman","60+",8),
(3,"Man","0-10",3),
(3,"Woman","0-10",10),
(3,"Man","10-30",3),
(3,"Woman","10-30",10),
(3,"Man","30-60",1),
(3,"Woman","30-60",9),
(3,"Man","60+",8),
(3,"Woman","60+",5)
}
使用这个table,我可以计算出每组的消费金额。 'group' 可以用不同的方式定义:'Men'(仅性别)或 'Men in age group <60'(性别 + 年龄组)或 'Men in age group <60 in week 25'(性别 + 年龄组 + 周数)等。为了计算每组花费的金额(在这种情况下,按性别+周数分组),我使用以下 dax 代码创建了一个新的 table:
grouped spending =
VAR spending =
GROUPBY('Sample Table',
'Sample Table'[Weeknumber],
'Sample Table'[Gender],
"sum_spent_group",
SUMX(CURRENTGROUP(), 'Sample Table'[spent]))
RETURN
spending
并向此 table 添加一列以计算所选组花费的部分占总花费的金额:
spending part =
DIVIDE(CALCULATE(SUM('grouped spending'[sum_spent_group]),
ALLEXCEPT('grouped spending','grouped spending'[group])),
SUM('grouped spending'[sum_spent_group])
)
现在我可以显示一个组的支出占总支出的百分比:
有没有办法让用户选择(使用 buttons/bookmarks/etc..?)选择哪些列应该在 groupby 中?换一种说法;是否可以使此代码更通用,并让用户指定组组合?以及分子作为分母。
我意识到一种解决方案是为所有组组合制作列,但在我的真实数据中,我有更多的类别,所以这将花费很多 space(以及很多工作.. ).
非常感谢!
如果我没理解错的话,您实际上不需要为每个组创建单独的 table。
- 创建一个度量来计算将受切片器(您的组)影响的支出总和。类似于
spent_calc = SUM('Sample Table'[spent])
- 创建一个度量来计算总支出,而不管选择的切片器如何
CALCULATE(SUM('Sample Table'[spent]),ALL('Sample Table'))
- 创建计算总花费百分比的度量值
%_spent_by_selected_group = DIVIDE([spent_calc],[total_spent])
- 创建切片器并将百分比度量放入所需的视觉对象中,例如
现在用户可以创建属性的任意组合并查看花费总额的百分比。需要注意的是,如果你有很多类别,你当然必须有很多切片器,所以它可能不是最优雅的解决方案。不过可能是最简单的一个。