如何在Power Pivot中创建DAX度量,该度量显示用作数据透视表中每行的最低级别过滤器上下文的列名称?
我有两个与“帐户”列相关的表格。
表DimAccount
Account,Model_L1,Model_L2,Model_L3,Business_L1,Business_L2,Business_L3
1,Working Capital,Trade Payables,Trade Creditors,Opex,Technology,Managed Services
2,Investing,Capex Creditors and Accruals,Capex Trade Creditors,Capex,Capex Creditors,Intangible Asset Creditors
3,Working Capital,Trade Receivables,Prepaid Debtors,Receivables,Prepaid Receipts,Prepaid MBB Receipts
表格BalSheet
Account,Value
1,50
2,60
3,40
用于计算上下文过滤器的DAX度量
MEASURE BalSheet[CurrentLevel]
=SWITCH(TRUE(),
CALCULATE(COUNTROWS(VALUES(DimAccount[Model_L3])),
ALLEXCEPT(DimAccount, DimAccount[Model_L3])) = 1,
"Model_L3",
CALCULATE(COUNTROWS(VALUES(DimAccount[Model_L2])),
ALLEXCEPT(DimAccount, DimAccount[Model_L2])) = 1,
"Model_L2",
CALCULATE(COUNTROWS(VALUES(DimAccount[Model_L1])),
ALLEXCEPT(DimAccount, DimAccount[Model_L1])) = 1,
"Model_L1",
CALCULATE(COUNTROWS(VALUES(DimAccount[Business_L3])),
ALLEXCEPT(DimAccount, DimAccount[Business_L3])) = 1,
"Business_L3",
CALCULATE(COUNTROWS(VALUES(DimAccount[Business_L2])),
ALLEXCEPT(DimAccount, DimAccount[Business_L2])) = 1,
"Business_L2",
CALCULATE(COUNTROWS(VALUES(DimAccount[Business_L1])),
ALLEXCEPT(DimAccount, DimAccount[Business_L1])) = 1,
"Business_L1",
"All"
)
单层次结构
该措施适用于单一层次结构。这是数据透视表的样子(在csv中)。
Model_L1,Model_L2,Model_L3,CurrentLevel
Investing,Capex Creditors and Accruals,Capex Trade Creditors,Model_L3
Investing,Capex Creditors and Accruals,,Model_L2
Investing,,,Model_L1
Working Capital,Trade Payables,Trade Creditors,Model_L3
Working Capital,Trade Payables,,Model_L2
Working Capital,Trade Receivables,Prepaid Debtors,Model_L3
Working Capital,Trade Receivables,,Model_L2
Working Capital,,,Model_L1
Grand Total,,,All
混合层次结构
当我将2个层次结构混合在一起时(例如,模型和业务),当度量应显示“Business_L2”或“Business_L3”时,该度量将持续显示“Model_L1”。这是数据透视表的样子(在csv中)。
Model_L1,Business_L2,Business_L3,CurrentLevel
Investing,Capex Creditors,Intangible Asset Creditors,Model_L1
Investing,Capex Creditors Total,,Model_L1
Investing,,,Model_L1
Working Capital,Prepaid Receipts,Prepaid MBB Receipts,Model_L1
Working Capital,Prepaid Receipts Total,,Model_L1
Working Capital,Technology,Managed Services,Model_L1
Working Capital,Technology Total,,Model_L1
Working Capital,,,Model_L1
Grand Total,,,All
这就是我想要数据透视表的样子:
Model_L1,Business_L2,Business_L3,CurrentLevel
Investing,Capex Creditors,Intangible Asset Creditors,Business_L3
Investing,Capex Creditors Total,,Business_L2
Investing,,,Model_L1
Working Capital,Prepaid Receipts,Prepaid MBB Receipts,Business_L3
Working Capital,Prepaid Receipts Total,,Business_L2
Working Capital,Technology,Managed Services,Business_L3
Working Capital,Technology Total,,Business_L2
Working Capital,,,Model_L1
Grand Total,,,All
答案 0 :(得分:0)
当您将“Model_L1”,“Business_L2”,“Business_3”作为数据透视表行字段时,[CurrentLevel]度量始终求值为“Model_L1”的原因是因为“Model_L1”位于“Business_X”之上SWITCH()语句。这应该会给你想要的结果:
=SWITCH(TRUE(),
CALCULATE(COUNTROWS(VALUES(DimAccount[Model_L3])),
ALLEXCEPT(DimAccount, DimAccount[Model_L3])) = 1,
"Model_L3",
CALCULATE(COUNTROWS(VALUES(DimAccount[Business_L3])),
ALLEXCEPT(DimAccount, DimAccount[Business_L3])) = 1,
"Business_L3",
CALCULATE(COUNTROWS(VALUES(DimAccount[Model_L2])),
ALLEXCEPT(DimAccount, DimAccount[Model_L2])) = 1,
"Model_L2",
CALCULATE(COUNTROWS(VALUES(DimAccount[Business_L2])),
ALLEXCEPT(DimAccount, DimAccount[Business_L2])) = 1,
"Business_L2",
CALCULATE(COUNTROWS(VALUES(DimAccount[Model_L1])),
ALLEXCEPT(DimAccount, DimAccount[Model_L1])) = 1,
"Model_L1",
CALCULATE(COUNTROWS(VALUES(DimAccount[Business_L1])),
ALLEXCEPT(DimAccount, DimAccount[Business_L1])) = 1,
"Business_L1",
"All"
)