cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
Arne_R
Helper II
Helper II

Sum in collection with condition (filter)

Hello!

I am trying to sum a column in a collection with a condition.

I have a collection: 'WerkbonPos_col' . Then I create a new collection, based on the previously mentioned collection. Line numbers are added to this, among other things.

 

Now I want the sum of the 'Werktijd' (working hours), where the condition must be the 'Personeelsnummer' (employee number).
The formula gives me the total sum of all lines, independent of the employee number.

 

Im trying this: 

// Behandelen van de werken
Clear(WerkbonPOS_col_Indexed);;
ForAll(
    WerkbonPOS_col;
    Collect(
        WerkbonPOS_col_Indexed;
        Patch(
            Last(
                FirstN(
                    WerkbonPOS_col;
                    CountRows(WerkbonPOS_col_Indexed) + 1
                )
            );
            {
                Lijnnummer: (CountRows(WerkbonPOS_col_Indexed) + 1) * 10;
                Totale_km: Sum(Filter(WerkbonPOS_col;Personeelsnummer = ThisRecord.Personeelsnummer);Km); 
                Totale_uren: Sum(Filter(WerkbonPOS_col; Personeelsnummer = ThisRecord.Personeelsnummer);Werktijd);
                Documentnaam: "P" & ThisRecord.Personeelsnummer & "N" & var_werkbonnummer & "D" & Concat(
                    Split(
                        var_werkbon_datum;
                        "/"
                    );
                    Result
                );
                WerkbonKey: "P" & ThisRecord.Personeelsnummer & "N" & var_werkbonnummer & "D" & Concat(
                    Split(
                        var_werkbon_datum;
                        "/"
                    );
                    Result
                )
            }
        )
    )
);;


Everything works except following lines:

Totale_km: Sum(Filter(WerkbonPOS_col;Personeelsnummer = ThisRecord.Personeelsnummer);Km);
Totale_uren: Sum(Filter(WerkbonPOS_col; Personeelsnummer = ThisRecord.Personeelsnummer);Werktijd);

 

Part of 'WerkbonPos_col': 

PersoneelsnummerWerktijd
079

2

079

3

323

3

 

What I have after the code ('WerkbonPos_col_Indexed'):

PersoneelsnummerWerktijdTotale_uren
079

2

8

079

3

8

323

3

8

 

What I want: 

PersoneelsnummerWerktijdTotale_uren
079

2

5

079

3

5

323

3

3

 

Can someone tell me what I'm doing wrong?

Thank you in advance!

1 ACCEPTED SOLUTION

Accepted Solutions
v-liwei-msft
Microsoft
Microsoft

Hi @Arne_R ,

 

I suggest you use this function:
 
Collect(
WerkbonPos_col_Indexed,
Ungroup( 
AddColumns( 
GroupBy( 
WerkbonPos_col,
"Personeelsnummer",
"fenzu"
),
"Totale_uren",
Sum( 
fenzu,
Werktijd
)
),
"fenzu"
)
vliweimsft_0-1652350149750.png

 

Best Regards,

Levi

View solution in original post

3 REPLIES 3
Arne_R
Helper II
Helper II

If I 'hard-code' the 'Personeelsnummer to 079, it works for employee 079. 

Totale_km: Sum(Filter(WerkbonPOS_col;Personeelsnummer = "079");Km); 
Totale_uren: Sum(Filter(WerkbonPOS_col; Personeelsnummer = "079");Werktijd);

 

Example: 

Part of 'WerkbonPos_col': 

PersoneelsnummerWerktijd
079

2

079

3

323

3

 

What I have after the code ('WerkbonPos_col_Indexed'):

PersoneelsnummerWerktijdTotale_uren
079

2

5

079

3

5

323

3

5

 

What I want: 

PersoneelsnummerWerktijdTotale_uren
079

2

5

079

3

5

323

3

3

 

 

v-liwei-msft
Microsoft
Microsoft

Hi @Arne_R ,

 

I suggest you use this function:
 
Collect(
WerkbonPos_col_Indexed,
Ungroup( 
AddColumns( 
GroupBy( 
WerkbonPos_col,
"Personeelsnummer",
"fenzu"
),
"Totale_uren",
Sum( 
fenzu,
Werktijd
)
),
"fenzu"
)
vliweimsft_0-1652350149750.png

 

Best Regards,

Levi

Thank you. The code works for me. I adapt it to suit my needs.

 

Kind regards
Arne

Helpful resources

Announcements
October Events

Mark Your Calendars

So many events that are happening this month - don't miss out!

Ignite 2022

WHAT’S NEXT AT MICROSOFT IGNITE 2022

Explore the latest innovations, learn from product experts and partners, level up your skillset, and create connections from around the world.

Power Apps Africa Challenge 2022

Power Apps Africa Challenge

Your chance to join an engaging competition of Power Platform enthusiasts.

Users online (3,580)