简体   繁体   English

如何根据 PowerBi 中的日期过滤器对数据求和

[英]How to SUM data based on date filters in PowerBi

My sample CSV data looks like this, So that you can use the same data below:我的示例 CSV 数据如下所示,因此您可以使用以下相同的数据:

ID,Project,From,To,Percentage
1,APPLE,01-01-2022,31-03-2022,50
1,MICROSOFT,01-01-2022,15-01-2022,50
1,MICROSOFT,01-02-2022,28-02-2022,50
1,MICROSOFT,01-03-2022,31-03-2022,50
2,ORACLE,01-02-2022,23-06-2022,50
3,APPLE,23-04-2022,23-06-2022,100
1,MICROSOFT,16-01-2022,31-01-2022,50
2,DELL,01-12-2021,01-04-2022,50

I added a new column for which the dax is我添加了一个新列,其中 dax 是

CALCULATE(SUM('sample set'[Percentage]), FILTER('sample set', 'sample set'[ID]=EARLIER('sample set'[ID])&&EARLIER('sample set'[From]) <= 'sample set'[To]&&EARLIER('sample set'[To])>='sample set'[From]))

The result is as shown below结果如下图

在此处输入图像描述

My objective is to see if on any date any employee has been allocated for more than 100% in work.我的目标是查看在任何日期是否有任何员工被分配超过 100% 的工作。 As you can see employee id 1 has been allocated 1/Jan/22 to 31/Mar/22 for 50% in APPLE and multiple periods in MICROSOFT for 50% itself but it doesn't for any period is greater than 100%.如您所见,员工 id 1 已在 APPLE 中分配 50% 的 50% 的 1/Jan/22 到 31/Mar/22 ,并在 MICROSOFT 中分配了 50% 本身的多个时期,但在任何时期都没有超过 100%。

But in the first line, the allocated percentage is shown as 250%.但在第一行,分配的百分比显示为 250%。 It is because the filter criteria meet all the sub-periods for ID 1. but if you really look into it, it is not 100% on any sub-period.这是因为过滤条件满足 ID 1 的所有子周期。但如果您真正查看它,它不是任何子周期的 100%。 So is there a way that I can get a TRUE/FALSE or any output that can help me see if any employee has been allocated for any project during a certain time that the allocated percentage sum is more than 100%?那么有没有一种方法可以让我获得 TRUE/FALSE 或任何可以帮助我查看在分配百分比总和超过 100% 的特定时间内是否有任何员工被分配到任何项目的输出?

(1) Create a calendar table (1) 创建日历表

Create a calendar table that is not associated with your model.创建一个与您的模型无关的日历表。 The easiest way is to just go into the Modeling ribbon and click 'new table.'最简单的方法是进入建模功能区并单击“新表”。 The DAX is simple -- if you want to pick your own begin/end dates use CALENDAR() and not CALENDARAUTO() : DAX 很简单——如果您想选择自己的开始/结束日期,请使用 CALENDAR() 而不是 CALENDARAUTO() :

dimCalendar = CALENDARAUTO(12)

(2) Create your measure to drive from the calendar table (2) 创建您的度量以从日历表驱动

Total daily percentage = 

CALCULATE(
    SUM(DemoData[Percentage])
    , FILTER (DemoData, MAX('dimCalendar'[Date]) >= DemoData[From] && MIN('dimCalendar'[Date]) <= DemoData[To])
)

set your measure alongside dimCalendar[Date] and [ID] -- or put it in a line chart visual or whatever.将您的度量与 dimCalendar[Date] 和 [ID] 一起设置 - 或将其放入可视化折线图或其他任何内容中。 You'll be able to quickly see where a line drifts over 100.您将能够快速查看直线漂移超过 100 的位置。

(Here I've added a line for ID #1 on Project 'Side-hustle') (在这里,我在“Side-hustle”项目中为 ID #1 添加了一行) 在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM