powerbi - How to Filter table and Limit it to 1 TOP row with Dax
问题描述
I'm new to Power BI and Dax queries. I have 2 tables added with 1:* relation. What I'm trying to do is to filter the second table by a foreign key and specific Key.
Example:###Table1:###
ID | RunID | ...[Other columns]
###Table2:###
ID | RunID | Key | Value | State
So what I'm trying to do in Power BI using Dax query is to get value for a key. (It's possible to have duplicated Key-Value pairs but I can take a top one)
NEWCOLUMN =
VAR tmp =
FILTER (
Table2,
AND ( Table2[RunID] = Table1[RunID], Table2[Key] = "KEY_I_NEED" )
)
VAR tmp2 =
CALCULATE ( VALUES ( Table2[Value] ), TOPN ( 1, tmp ) )
RETURN
tmp2
Right now I'm getting the error:
"The expression refers to multiple columns. Multiple columns cannot be converted to a scalar value."
and couldn't get it to work the way I need.
解决方案
If there is only one distinct value (duplicates are fine) then you can use a lookup:
NEWCOLUMN =
LOOKUPVALUE(
Table2[Value],
Table2[RunID], Table1[RunID],
Table2[Key], "KEY_I_NEED"
)
If there are multiple distinct values, then you can use a max:
NEWCOLUMN =
CALCULATE(
MAX( Table2[Value] ),
Table2[Key] = "KEY_I_NEED"
)
The above implicitly matches RunID
(assuming that's the columns the tables are related on).
There are plenty of other ways you could do this using various combinations of functions like TOPN
, MAXX
, RELATEDTABLE
, VALUES
, and so forth, but these aren't likely to be simpler or more efficient than what I've suggested.
推荐阅读
- javascript - 由于某种原因,我的导航栏有滚动条?
- mongodb - Kafka 主题返回 0 条消息
- css - 我的 css 文件太大,因为 url-loader 正在将 .jpg 代码插入到 .css 文件中 - 如何配置 webpack 使其指向 img?
- c# - Tweetinvi 5.0 获取推文回复
- git - 使用 git clone 从 github 下载大型 lfs 文件时出现问题
- java - 如何在 Firestore 中构建关注系统
- c# - 未找到 Xamarin 绑定属性
- javascript - Firestore 功能云消息传递无法正常工作
- python-3.x - matplotlib 子图自动缩小
- javascript - 在节点应用程序中使用 SocketIO 跨路由/页面发送数据