Power BI中如何按指定规则生成最高浏览量屏幕名称新列?
Create Calculated Column for Top Screen Name in Power BI
Got it, let's build this calculated column step by step to meet your exact requirements. Here's how you can do it with DAX:
DAX Formula
Top Screen Name = VAR CurrentID = [id] VAR CurrentStartDate = [start date] VAR TodayFilter = TODAY() -- Define the filtered group: today's data, same id+start date, and customer count = 1 VAR TargetGroup = CALCULATETABLE( 'YourTableName', -- Replace with your actual table name 'YourTableName'[date] = TodayFilter, 'YourTableName'[id] = CurrentID, 'YourTableName'[start date] = CurrentStartDate, 'YourTableName'[Customer Count] = 1 -- Replace with your customer count column name ) -- Get the maximum screen view count in the target group VAR MaxViews = MAXX(TargetGroup, 'YourTableName'[Screen View Count]) -- Replace with your view count column name -- Fetch the first screen name with the max view count (handles ties by picking the first one) VAR TopScreen = SELECTCOLUMNS( TOPN( 1, TargetGroup, 'YourTableName'[Screen View Count], DESC, -- Sort by view count descending 'YourTableName'[Screen Name], ASC -- Break ties by screen name ascending (adjust if you want original order) ), "ScreenName", 'YourTableName'[Screen Name] ) -- Return the result, or blank if no matching data RETURN IF(ISBLANK(MaxViews), BLANK(), TopScreen)
Key Explanations
- Variables: We use variables to break down the logic for readability.
CurrentIDandCurrentStartDateanchor us to the current row's group. - Filtered Group:
CALCULATETABLEnarrows down the data to today's records, matching the current id/start date, and where the customer count equals 1. - Max View Count:
MAXXgrabs the highest screen view count from the filtered group. - Handle Ties:
TOPN(1)ensures we pick the first screen name when multiple entries have the same max view count. The second sort parameter (Screen Name ASC) defines how ties are broken—if you prefer to use the original row order instead, you can add an index column to your table and sort by that instead. - Fallback: The final
IFreturns blank if there's no matching data for the group, avoiding errors.
Notes
- Don't forget to replace placeholders like
'YourTableName',[Screen View Count], and[Customer Count]with your actual table and column names. - If your "customer count" is a calculated measure instead of a column, adjust the
CALCULATETABLEfilter to useFILTER(TargetGroup, CALCULATE([Customer Count Measure]) = 1)instead.
内容的提问来源于stack exchange,提问作者KARTHIKEYAN.A
相关产品推荐
相关产品推荐

