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

Try your skills in the Power BI Dataviz World Championship! Round one ends June 26. Join now

Reply
Friedrich902
Regular Visitor

Get the first element from list which is not taken

Hello to everyone,

 

I want to take the first element from a list in the same row which was not taken from a row with a smaller index.

The Idea ist to match debit and credit bookings.

 

Do you know how the custom function in power query hat to be written?

 

In the sample data you can see a list where the first element should be taken but not twice.

 

Date | Booking Text | Transaction text | Account | Attribute | Value | IndexMatching Elements Index | Desired Result Custom Function

06.10.2021 00:00ACDTransactionADebit1209{7,9}7
06.10.2021 00:00ACDTransactionBDebit1203376{7,9}9
06.10.2021 00:00ACDTransactionCDebit5030415 null
06.10.2021 00:00ACDTransactionDDebit12040551{7,9}null

 

Thank you in advance.

1 ACCEPTED SOLUTION
AlienSx
Super User
Super User

Hello, @Friedrich902 

    s = your_table,
    lst = s[IndexMatching Elements Index],
    new_col = 
        List.Accumulate(
            lst,
            {},
            (s, c) => s & {List.Difference(c ?? {null}, List.RemoveNulls(s)){0} ? }
        ),
    result = Table.FromColumns( Table.ToColumns(s) & {new_col}, Table.ColumnNames(s) & {"Desired Result Custom Function"} )

View solution in original post

2 REPLIES 2
Friedrich902
Regular Visitor

Hello AlienSX, than you so much for the solution. 

I have an additional problem.

 

I want to add a column "Desired Result Custom Function 2" which takes all elements from the list "All Elements Index" which are not taken in the new column "Result Custom Function 1". Is this possible?

 

DateBooking TextTransaction textAccountValueAttributeIndexAll Elements IndexMatching Elements IndexResult Custom Function 1Desired Result Custom Function 2
06.10.2021ACDTransaction ADebitAccountA1209{7,8,9}{7,9}7null
06.10.2021ACDTransaction ADebitAccountA1203376{7,8,9}{7,9}9null
06.10.2021ACDTransaction ADebitAccountA5030415{7,8,9}  8
06.10.2021ACDTransaction ADebitAccountA12040551{7,8,9}{7,9} 8

 

My M Code so far:

let
    Source = #table({"Date", "Booking Text", "Transaction text", "Account", "Value", "Attribute", "Index", "All Elements Index", "Matching Elements Index"}, {{#date(2021,10,6), "ACD", "Transaction A", "Debit", "AccountA", 120, 9, {7, 8, 9} , {7,9}}, {#date(2021,10,6), "ACD", "Transaction A", "Debit", "AccountA", 120, 3376, {7, 8, 9} , {7,9}}, {#date(2021,10,6), "ACD", "Transaction A", "Debit", "AccountA", 50, 30415, {7, 8, 9} , null}, {#date(2021,10,6), "ACD", "Transaction A", "Debit", "AccountA", 120, 40551, {7, 8, 9} , {7,9}
}}),

#"Changed_Type1" = Table.TransformColumnTypes(Source,{{"Booking Text", type text}, {"Transaction text", type text}, {"Account", type text}, {"Attribute", type number}, {"Date", type date}, {"Value", type text}, {"Index", Int64.Type}}),

s = #"Changed_Type1",

lst = s[Matching Elements Index],
    new_col = 
        List.Accumulate(
            lst,
            {},
            (s, c) => s & {List.Difference(c ?? {null}, List.RemoveNulls(s)){0} ? }
        ),
    result = Table.FromColumns( Table.ToColumns(s) & {new_col}, Table.ColumnNames(s) & {"Desired Result Custom Function"} ),
    #"Changed Type" = Table.TransformColumnTypes(result,{{"Desired Result Custom Function", Int64.Type}})

    in 
    #"Changed Type"

 

AlienSx
Super User
Super User

Hello, @Friedrich902 

    s = your_table,
    lst = s[IndexMatching Elements Index],
    new_col = 
        List.Accumulate(
            lst,
            {},
            (s, c) => s & {List.Difference(c ?? {null}, List.RemoveNulls(s)){0} ? }
        ),
    result = Table.FromColumns( Table.ToColumns(s) & {new_col}, Table.ColumnNames(s) & {"Desired Result Custom Function"} )

Helpful resources

Announcements
Fabric Data Days is here Carousel

Fabric Data Days 2026

Don't miss out on Data Days, June 15 through August 7. Learn Fabric, Power BI, SQL, AI and more.

May Power BI Update Carousel

Power BI Monthly Update - May 2026

Check out the May 2026 Power BI update to learn about new features.

Power BI DataViz World Championships carousel

Power BI DataViz World Championships - June 2026

A new Power BI DataViz World Championship is coming this June! Don't miss out on submitting your entry.