且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在 Power BI 中计算累积值

更新时间:2022-12-09 18:30:50

为此,您需要一些构建块.这是我使用的数据模型:

You need a few building blocks for this. Here is the data model I used:

<编辑>我看到不同表中的 user_id 不一样,在这种情况下,您可以省略表之间的关系,并且 Calendar 表中的两个关系都将处于活动状态 - 无需更改关系count_of_customer 度量中的语义.</edit>

<edit> I see user_id in the different tables are not the same, in that case you can omit the relationship between the tables and the two relationships from the Calendar table will both be active - with no need to change the relationship semantics in the count_of_customer measure. </edit>

日历表很重要,因为我们不能依赖单个日期列来聚合来自不同表的数据,因此我们使用此示例 DAX 代码创建一个通用日历表:

The calendar table is important because we can't rely on one single date column to aggregate data from different tables, so we create a common calendar table with this sample DAX code:

Calendar = 
ADDCOLUMNS (
    CALENDARAUTO () ,
    "Year" , YEAR ( [Date] ) ,
    "Month" , FORMAT ( [Date] , "MMM" ) ,
    "Month-Year" , FORMAT ( [Date] , "MMM")&"-"&YEAR ( [Date] ) ,
    "YearMonthNo" , YEAR ( [Date] ) * 12 + MONTH ( [Date] ) - 1
)

确保按 YearMonthNo 列对 Month-Year 列进行排序,以便您的表格看起来不错:

Make sure to sort the Month-Year column by the YearMonthNo column so your tables look nice:

将您的关系设置为从 Calendaruser 的活动关系 - 如果不是,除非您在代码中相应地更改关系,否则这些措施将不起作用!在我的数据模型中,Calendarcustomer order 之间存在非活动关系.

Set your relationships as shown with the active relationship from Calendar to user - if not the measures will not work unless you alter the relationships accordingly in the code! In my data model the inactive relationship is between Calendar and customer order.

接下来是我们将为此使用的措施.首先我们统计用户数,一个简单的行数:

Next up are the measures we will use for this. First off we count the users, a simple row count:

count_of_users = COUNTROWS ( user )

然后我们统计order表中不同的用户id来统计客户,这里我们需要使用Calendarcustomer order之间的inactive关系> 为此,我们必须调用 CALCULATE:

Then we count distinct user ids in the order table to count customers, here we need to use the inactive relationship between Calendar and customer order and to do this we have to invoke CALCULATE:

count_of_customers = 
CALCULATE (
    DISTINCTCOUNT ( 'customer order'[user_id] ) ,
    USERELATIONSHIP (
        'Calendar'[Date] ,
        'customer order'[order_date]
    )
)

我们可以使用这个度量来累计统计用户数:

We can use this measure to count users cumulatively:

cumulative_users = 
VAR _maxVisibleDate = MAX ( 'Calendar'[Date] ) 
RETURN
CALCULATE ( 
    [count_of_users] , 
    ALL ( 'Calendar' ) , 
    'Calendar'[Date] <= _maxVisibleDate
)

这个衡量每月累积客户的方法:

And this measure to count cumulative customers per month:

cumulative_customers = 
VAR _maxVisibleDate = MAX ( 'Calendar'[Date] ) 
RETURN
CALCULATE ( 
    SUMX ( 
        VALUES ( 'Calendar'[YearMonthNo] ) ,
        [count_of_customers] 
    ),
    ALL ( 'Calendar' ) , 
    'Calendar'[Date] <= _maxVisibleDate
)

最后我们想要这些最后的累积度量的比率:

Lastly we want the ratio of these last cumulative measures:

cumulative_customers/users = 
DIVIDE (  
    [cumulative_customers] , 
    [cumulative_users] 
)

这是你的结果: