我正在处理地理层次结构。我希望折线图或功能区图表上的图例显示基于切片器选择的下一个最低相关详细级别。
目标: 没有“切片器选择”会产生区域图(东南,东北等线)。从切片机中选择“东北”地区会产生一个州图(缅因州,新罕布什尔州等的线)。从切片机中选择“缅因州”会产生一个县图,依此类推。
我可以在卡中结合使用HASONEVALUE和VALUES来获得动态单一值。但是,功能区或折线图的图例字段将不接受这种度量。
有什么方法可以基于切片器选择来创建带有动态图例的Power BI折线图或功能区图?
我不想为所有层次结构节点创建单独的度量,因为有数千种可能的叶子。
答案 0 :(得分:4)
假设我有一些美国最大的20个县的人口数据,如下所示:
Data = DATATABLE(
"Rank", INTEGER, "Region", STRING, "County", STRING, "State", STRING,
"2010", INTEGER, "2012", INTEGER, "2014", INTEGER,
{{1,"West","Los Angeles","California",9818605,9826773,9826773},
{2,"Midwest","Cook","Illinois",5194675,5199971,5199971},
{3,"South","Harris","Texas",4092459,4109362,4109362},
{4,"South","Maricopa","Arizona",3817117,3824834,3824834},
{5,"West","San Diego","California",3095313,3105115,3105115},
{6,"West","Orange","California",3010232,3017598,3017598},
{7,"South","Miami-Dade","Florida",2496435,2503034,2503034},
{8,"Northeast","Kings","New York",2504700,2508515,2508515},
{9,"South","Dallas","Texas",2368139,2375207,2375207},
{10,"Northeast","Queens","New York",2230722,2233895,2233895},
{11,"West","Riverside","California",2189641,2202978,2202978},
{12,"West","San Bernardino","California",2035210,2042027,2042027},
{13,"West","King","Washington",1931249,1937157,1937157},
{14,"West","Clark","Nevada",1951269,1953927,1953927},
{15,"South","Tarrant","Texas",1809034,1816850,1816850},
{16,"West","Santa Clara","California",1781642,1786267,1786267},
{17,"South","Broward","Florida",1748066,1752122,1752122},
{18,"South","Bexar","Texas",1714773,1723561,1723561},
{19,"Midwest","Wayne","Michigan",1820584,1815246,1815246},
{20,"Northeast","New York","New York",1585873,1587481,1587481}
})
使用此数据作为查询编辑器中的源,我们将生成两个表。第一个将是Fact
表,该表不显示年份,并且对前三个县来说是这样的:
第二个将是Bridge
表,该表只是唯一的Region
,State
,County
组合的列表:
我们将使用DAX创建一个最终表。这将是我们的Legend
表。
Legend = UNION(SUMMARIZECOLUMNS(Fact[Region], Fact[Rank], "Level", 1),
SUMMARIZECOLUMNS(Fact[State], Fact[Rank], "Level", 2),
SUMMARIZECOLUMNS(Fact[County], Fact[Rank], "Level", 3)
)
确保在Rank
列上设置了关系,以便您的关系图如下所示:
现在,我们将编写一种措施,利用我们刚刚在Legend
表中创建的这些级别。
Measure =
VAR StateCount = CALCULATE(DISTINCTCOUNT(Bridge[State]),
ALLSELECTED(Bridge), ALLSELECTED(Legend))
VAR RegionCount = CALCULATE(DISTINCTCOUNT(Bridge[Region]),
ALLSELECTED(Bridge), ALLSELECTED(Legend))
VAR LevelNumber = SWITCH(TRUE(), StateCount = 1, 3, RegionCount = 1, 2, 1)
RETURN CALCULATE(SUM(Fact[Population]), Legend[Level] = LevelNumber)
基本上,我们只想显示适当水平的人口。
在折线图上使用此度量,图例框中的Legend[Region]
和轴上的Fact[Year]
,当切片器留为空白时,结果如下所示:
当我们选择一个区域然后选择一个州时,我们得到以下信息:
这是我为此创建的PBIX文件的链接:Variable Legend.pbix