在Power BI中为dEquipments表创建含最新Reference的计算列
在Power BI的dEquipments表中创建最新安装位置计算列
前提准备
确保表之间已建立正确关系:
- dEquipments[Serial Number] ↔ fPlacesOfInstallation[Serial Number](一对多)
- fPlacesOfInstallation[Reference] ↔ dCalendar[Reference](一对多)
计算列DAX表达式
在dEquipments表中新建计算列,使用以下DAX代码:
Latest Installation Reference = VAR CurrentSerial = dEquipments[Serial Number] // 获取当前设备的所有安装位置Reference VAR InstallRefs = CALCULATETABLE( VALUES(fPlacesOfInstallation[Reference]), fPlacesOfInstallation[Serial Number] = CurrentSerial ) // 找到这些安装记录对应的最新日期 VAR LatestInstallDate = MAXX( CALCULATETABLE(dCalendar, dCalendar[Reference] IN InstallRefs), dCalendar[Date] ) // 匹配最新日期对应的安装位置Reference RETURN CALCULATE( VALUES(fPlacesOfInstallation[Reference]), fPlacesOfInstallation[Serial Number] = CurrentSerial, RELATED(dCalendar[Date]) = LatestInstallDate )
特殊场景处理
如果同一设备在同一日期有多个安装记录,VALUES会返回包含多个值的列表,可改用SELECTEDVALUE返回单个值或指定默认值:
Latest Installation Reference = VAR CurrentSerial = dEquipments[Serial Number] VAR InstallRefs = CALCULATETABLE( VALUES(fPlacesOfInstallation[Reference]), fPlacesOfInstallation[Serial Number] = CurrentSerial ) VAR LatestInstallDate = MAXX( CALCULATETABLE(dCalendar, dCalendar[Reference] IN InstallRefs), dCalendar[Date] ) RETURN SELECTEDVALUE( CALCULATETABLE( fPlacesOfInstallation[Reference], fPlacesOfInstallation[Serial Number] = CurrentSerial, RELATED(dCalendar[Date]) = LatestInstallDate ), "无有效安装记录" // 自定义无记录时的显示文本 )
内容的提问来源于stack exchange,提问作者Arnon Vichy
相关产品推荐
相关产品推荐

