Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
Anonymous
Not applicable

Sum values based on a date range withing a date range

Hi 

 

I have an employee table with their working pensum like

EmployeeFromToPensum
Aileen2019/01/012019/01/31100%
Aileen2019/02/012019/12/3180%
Barbara2019/01/012019/12/3160%
John2019/06/062019/10/31100%

 

In another table I manage the working periods (weeks per month)

PeriodFromTo
Period 012019/01/012019/01/06
Period 022019/01/072019/01/13
Period 032019/01/14

2019/01/20

 

Now I want to add to the second table a measure which sums the working pensum per period over all employees or if only one is selected kind of "lookup" his pensum for the period.

 

Thanks for your help

zemi

1 ACCEPTED SOLUTION
Anonymous
Not applicable

Hi @Anonymous 

Many thanks for input and ideas. Actually it does very close to what I need and was of great help.

 

But I found a smarter and also nicer way. This code actually works (in German but I guess you can read it):

Arbeitspensum = 
CALCULATE (
    SUM ( Pensum[Arbeitspensum] );
    FILTER (
        ALL ( Pensum[Von]; Pensum[Bis] );
        SELECTEDVALUE ( PeriodenKalender[Von] ) >= Pensum[Von] && SELECTEDVALUE ( PeriodenKalender[Bis] ) <= Pensum[Bis] && 
        SELECTEDVALUE ( PeriodenKalender[Bis] ) >= Pensum[Von] && SELECTEDVALUE ( PeriodenKalender[Bis] ) <= Pensum[Bis]
    )
)
It adds the following red data 
PeriodFromToWorkPensum if all are selected WorkPensum for Aileen only
Period 012019/01/012019/01/06160%100%
Period 022019/01/072019/01/13160%100%
Period 032019/01/14

2019/01/20

160%

100%

... 

 

...

...

Period 062019/02/01

...

140%

80%

... 

 

...

...

Period 272019/06/06

...

240%

80%

... 

 

...

...

 
Thanks again, zemi

View solution in original post

7 REPLIES 7
Anonymous
Not applicable

Can you see if this is what you had in mind?

 

Total Pensum = sum( Employee[Pensum] )


Measure = 
CALCULATE( 
    [Total Pensum], 
          FILTER( 
            Employee ,
            Employee[Start]>= MAX(WorkingPeriods[Start])
            && Employee[End] >= MAX(WorkingPeriods[Start])
        )
    )

Table between Dates.png

 

in my measure, Start = From and End = To

 

Anonymous
Not applicable

Hi @Anonymous 

No, but maybe close.


Aileen works 100% in period 1 and Barbara works 60%. So it should sum up to 160%.

Feb 1st onward it wouldbe 80% + 60%

June 6th = 80 + 60 + 100

 

I ended up with (

Measure =
CALCULATE (
SUM ( Pensum[Arbeitspensum] );
FILTER (
ALL ( Pensum );
SELECTEDVALUE ( PeriodenKalender[Von] ) >= Pensum[Von] && SELECTEDVALUE ( PeriodenKalender[Bis] ) <= Pensum[Bis] &&
SELECTEDVALUE ( PeriodenKalender[Bis] ) >= Pensum[Von] && SELECTEDVALUE ( PeriodenKalender[Bis] ) <= Pensum[Bis]
))
But that just sums up any value 😞
 
Thanks anyway, zemi

 

Anonymous
Not applicable

I see. So the Weeks per month table has many more rows then?  

Anonymous
Not applicable

Hi @Anonymous 

Yes, the table goes down till the end of the year. Also employees we have many more 🙂 Sorry for not mentioning this.

zemi

Anonymous
Not applicable

@Anonymous ,

No problem at all, just wanted to sure I wasn't missing anything 🙂

 

I will try to work on this today for you

Anonymous
Not applicable

@Anonymous 

I think this will be closer to what you had in mind:

Final Table.png

There's a few steps to get that point, so we'll start in Power Query first:

  1. I created a DimEmployee Table with an ID for each employee. And then Merged with the actual employee table to get the ID in that table and then removed the employee name.  Proabably not needed, but was driving me crazy 🙂
  2. Extended out the WorkingPeriods table
  3. Then I added a column that will give me a list of all the dates in each period. This is more or less your date table, so I wanted to treat it as such
List.Dates(
[Start],
Duration.Days( [End]-[Start]) +1,
#duration(1,0,0,0)
)

 

  1. Then just expand that out, remove the Start and End date columns since we no longer need them and you are all set to load them.
  2. Related Working Periods to Employee and DimEmployee to Employee tables

 

Put Period Label on rows from working period and then measure:

Sum Based on Range = 
IF( 
    NOT( 
       ISBLANK( [Total Pensum] )
    ),
    SUMX( VALUES( DimEmployee),
        CALCULATE(
            [Total Pensum],
            CALCULATETABLE(
                Employee,
                FILTER( 
                    ALL(WorkingPeriods),
                    CALCULATE( MIN( Employee[Start]))  <=   MAX( WorkingPeriods[Dates])
                    &&
                    CALCULATE( max( Employee[END]))  >=   MAX( WorkingPeriods[Dates])
                )
            )
        )
    )
)

Here is the PBIX:

https://1drv.ms/u/s!Amqd8ArUSwDS0Ftkhm57vGRV_6K1

 

Let me know what you think!



 

Anonymous
Not applicable

Hi @Anonymous 

Many thanks for input and ideas. Actually it does very close to what I need and was of great help.

 

But I found a smarter and also nicer way. This code actually works (in German but I guess you can read it):

Arbeitspensum = 
CALCULATE (
    SUM ( Pensum[Arbeitspensum] );
    FILTER (
        ALL ( Pensum[Von]; Pensum[Bis] );
        SELECTEDVALUE ( PeriodenKalender[Von] ) >= Pensum[Von] && SELECTEDVALUE ( PeriodenKalender[Bis] ) <= Pensum[Bis] && 
        SELECTEDVALUE ( PeriodenKalender[Bis] ) >= Pensum[Von] && SELECTEDVALUE ( PeriodenKalender[Bis] ) <= Pensum[Bis]
    )
)
It adds the following red data 
PeriodFromToWorkPensum if all are selected WorkPensum for Aileen only
Period 012019/01/012019/01/06160%100%
Period 022019/01/072019/01/13160%100%
Period 032019/01/14

2019/01/20

160%

100%

... 

 

...

...

Period 062019/02/01

...

140%

80%

... 

 

...

...

Period 272019/06/06

...

240%

80%

... 

 

...

...

 
Thanks again, zemi

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.