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

calculated collum lookup latest "workers" by "customer ID"

can someone please help me with this table:

 

i need a calculated collum that gives me the most recent "workers" for each "customer ID"Capture.JPG

 

can someone help me please?

thanks

 

 

1 ACCEPTED SOLUTION

Hi @Anonymous 

Are you using the code in a calculated column (and not a measure)? If so, that error means there is more than one row with the same Customer ID and the latest date, with different numbers in Workers. You'll need to decide what to do in that case. If you for instance want the maximum, just change the DISTINCT( ) for MAX()

 

Calc_Column = 
VAR lates_date_ = CALCULATE(MAX(Table1[Order Data]); ALLEXCEPT(Table1;Table1[Customer ID]))
RETURN
CALCULATE(MAX(Table1[Workers]);ALLEXCEPT(Table1;Table1[Customer ID]);Table1[Order Data] = lates_date_)

 

 Please mark the question solved when done and consider giving kudos if posts are helpful.

Contact me privately for support with any larger-scale BI needs

Cheers 

 

SU18_powerbi_badge

View solution in original post

8 REPLIES 8
Greg_Deckler
Super User
Super User

Should be along the lines of:

 

Calculated collumn =
  MAXX(FILTER('Table',[Customer ID] = EARLIER([Customer ID]),[Workers])

@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...
AlB
Super User
Super User

Hi @Anonymous 

Do you want to do this in Power Query or DAX?  Can you paste the table in tabular-text format rather than in a screen cap, so thadt it can be copied?

Please mark the question solved when done and consider giving kudos if posts are helpful.

Contact me privately for support with any larger-scale BI needs

Cheers 

SU18_powerbi_badge

Anonymous
Not applicable

Here it is 🙂

I am trying to do in power query but, if you don´t mind, pleaso also include the solution in DAX.

 

EntityCustomer IDOrder IDOrder DataWorkersCalculated collum
A123451503332015-01-0116 
A1234515172010-01-0120 
B3453268122012-01-019 
A12345125542014-01-0140 
B3453268122019-01-0187 

Hi @Anonymous 

You can copy this in a blank query and see the steps, the take from there what you are interested in. I've built an intermediate column with the latest date per Customer ID. It is not strictly necessary but it helps to make the code more readable

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("fcxLCoAwDATQu2RdId9+lnqNkvtfw9RCFgpCYIbwmDnhhALEorbSUESiMJIdSHHrW8HLG1LbDJMxPuyKGkg4snbizTjZ+I6xmW6myfRvbSTrDdxv", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [Entity = _t, #"Customer ID" = _t, #"Order ID" = _t, #"Order Data" = _t, Workers = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Entity", type text}, {"Customer ID", Int64.Type}, {"Order ID", Int64.Type}, {"Order Data", type date}, {"Workers", Int64.Type}}),
    #"Added Custom" = Table.AddColumn(#"Changed Type", "Latest_date", each List.Max(Table.SelectRows(#"Changed Type",(inner)=>[Customer ID]=inner[Customer ID])[Order Data])),
    #"Added Custom1" = Table.AddColumn(#"Added Custom", "Calc_Column", each Table.SelectRows(#"Added Custom",(inner)=>([Customer ID]=inner[Customer ID] and inner[Order Data]=inner[Latest_date]))[Workers]{0}),
    #"Removed Columns" = Table.RemoveColumns(#"Added Custom1",{"Latest_date"})
in
    #"Removed Columns"

 Please mark the question solved when done and consider giving kudos if posts are helpful.

Contact me privately for support with any larger-scale BI needs

Cheers 

SU18_powerbi_badge

@Anonymous 

And the calculated column in DAX  (simpler, DAX is better equipped for this type of operations):

Calc_Column = 
VAR lates_date_ = CALCULATE(MAX(Table1[Order Data]); ALLEXCEPT(Table1;Table1[Customer ID]))
RETURN
CALCULATE(DISTINCT(Table1[Workers]);ALLEXCEPT(Table1;Table1[Customer ID]);Table1[Order Data] = lates_date_)

Please mark the question solved when done and consider giving kudos if posts are helpful.

Contact me privately for support with any larger-scale BI needs

Cheers 

 

SU18_powerbi_badge

 

Anonymous
Not applicable

@AlB 

when i paste the DAX function that you have supplied in the new custom collum created, appears an error message "A table of multiple values was supplied where a single value was expected"

 

can you help me?

 

thanks

 

 

Hi @Anonymous 

Are you using the code in a calculated column (and not a measure)? If so, that error means there is more than one row with the same Customer ID and the latest date, with different numbers in Workers. You'll need to decide what to do in that case. If you for instance want the maximum, just change the DISTINCT( ) for MAX()

 

Calc_Column = 
VAR lates_date_ = CALCULATE(MAX(Table1[Order Data]); ALLEXCEPT(Table1;Table1[Customer ID]))
RETURN
CALCULATE(MAX(Table1[Workers]);ALLEXCEPT(Table1;Table1[Customer ID]);Table1[Order Data] = lates_date_)

 

 Please mark the question solved when done and consider giving kudos if posts are helpful.

Contact me privately for support with any larger-scale BI needs

Cheers 

 

SU18_powerbi_badge

Anonymous
Not applicable

Many thanks @AlB !!!

It worked perfect 🙂

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.

Top Solution Authors
Top Kudoed Authors