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
Rondel
Frequent Visitor

Return the value based on earliest date creation and by group

Hello PBI Community,

 

I have a table with 3 columns (Group, Value Entered, Created On).  I want to create a 4th column (Original date) based on the earliest date creation by group.

GroupValue EnteredCreated On

Original Date

(*Desired Column)

x1/1/20244/11/20241/1/2024
x3/4/20244/12/20241/1/2024
x2/2/20245/13/20241/1/2024
y3/2/20241/1/20243/2/2024
y1/24/20241/2/20243/2/2024
2 ACCEPTED SOLUTIONS

ah, got it.

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WqlDSUTLUN9Q3MjAyATJN9A2h7FgdiKSxvgmSpBGqJJALkzTVNzRGSFaCdcIl4TbA5IBcE4QkzNRYAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Group = _t, #"Value Entered" = _t, #"Created On" = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Group", type text}, {"Value Entered", type date}, {"Created On", type date}}),
    #"Grouped Rows" = Table.Group(#"Changed Type", {"Group"}, {{"Rows", each _, type table [Group=nullable text, Value Entered=nullable date, Created On=nullable date]}, {"Min Created", each List.Min([Created On]), type nullable date}}),
    #"Expanded Rows" = Table.ExpandTableColumn(#"Grouped Rows", "Rows", {"Value Entered", "Created On"}, {"Value Entered", "Created On"}),
    #"Added Custom" = Table.AddColumn(#"Expanded Rows", "Original Date", (k)=> Table.SelectRows(#"Expanded Rows", each [Group]=k[Group] and [Created On]=k[Min Created]){0}[Value Entered], type date),
    #"Removed Other Columns" = Table.SelectColumns(#"Added Custom",{"Group", "Value Entered", "Created On", "Original Date"})
in
    #"Removed Other Columns"

How to use this code: Create a new Blank Query. Click on "Advanced Editor". Replace the code in the window with the code provided here. Click "Done". Once you examined the code, replace the Source step with your own source.

View solution in original post

v-yangliu-msft
Community Support
Community Support

Thanks for the reply from @lbendlin  , please allow me to provide another insight: 

 

Hi  @Rondel ,

 

Here are the steps you can follow:

1. Create calculated column.

Original Date =
var _mindate=
MINX(
    FILTER(ALL('Table'),
    'Table'[Group]=EARLIER('Table'[Group])),[Created On])
return
MINX(
    FILTER(ALL('Table'),
    'Table'[Created On]=_mindate),'Table'[Value Entered])

2. Result:

vyangliumsft_0-1713517571982.png

 

Best Regards,

Liu Yang

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly

View solution in original post

10 REPLIES 10
v-yangliu-msft
Community Support
Community Support

Thanks for the reply from @lbendlin  , please allow me to provide another insight: 

 

Hi  @Rondel ,

 

Here are the steps you can follow:

1. Create calculated column.

Original Date =
var _mindate=
MINX(
    FILTER(ALL('Table'),
    'Table'[Group]=EARLIER('Table'[Group])),[Created On])
return
MINX(
    FILTER(ALL('Table'),
    'Table'[Created On]=_mindate),'Table'[Value Entered])

2. Result:

vyangliumsft_0-1713517571982.png

 

Best Regards,

Liu Yang

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly

@v-yangliu-msft @lbendlin - I want to sincerely thank you both for your help and support!  

I learned a ton from both of your recommendations and was able to implement it to our business problem!

lbendlin
Super User
Super User

Your sample data is inconsistent with your request.  Which column is "earliest date creation"  based on?  Why not 1/24/2024 for group y ?

Hi @lbendlin 

the column [Created On] would be the system generated date.

1/24/2024 would not be for group y since it was entered on 1/2/2024 (oldest date for group y is  1/1/2024).

 

Hope this helps.

Please explain how you arrive at 3/2/2024 for group y.

Sure thing -

3/2/2024 is the date value entered by an operator.  This same value was created (system generated) on 1/1/2024. 

 

3/2/2024 is the [Original Date] because the following day (1/2/2024), they changed the date from 3/2/2024 to 1/24/2024.

ah, got it.

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WqlDSUTLUN9Q3MjAyATJN9A2h7FgdiKSxvgmSpBGqJJALkzTVNzRGSFaCdcIl4TbA5IBcE4QkzNRYAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Group = _t, #"Value Entered" = _t, #"Created On" = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Group", type text}, {"Value Entered", type date}, {"Created On", type date}}),
    #"Grouped Rows" = Table.Group(#"Changed Type", {"Group"}, {{"Rows", each _, type table [Group=nullable text, Value Entered=nullable date, Created On=nullable date]}, {"Min Created", each List.Min([Created On]), type nullable date}}),
    #"Expanded Rows" = Table.ExpandTableColumn(#"Grouped Rows", "Rows", {"Value Entered", "Created On"}, {"Value Entered", "Created On"}),
    #"Added Custom" = Table.AddColumn(#"Expanded Rows", "Original Date", (k)=> Table.SelectRows(#"Expanded Rows", each [Group]=k[Group] and [Created On]=k[Min Created]){0}[Value Entered], type date),
    #"Removed Other Columns" = Table.SelectColumns(#"Added Custom",{"Group", "Value Entered", "Created On", "Original Date"})
in
    #"Removed Other Columns"

How to use this code: Create a new Blank Query. Click on "Advanced Editor". Replace the code in the window with the code provided here. Click "Done". Once you examined the code, replace the Source step with your own source.

@lbendlin 

Would you mind explaining what this part of the code does?  I can't seem to make it work on my end: 

 

 

Table.AddColumn(#"Expanded Rows", "Original Date", (k)=> Table.SelectRows(#"Expanded Rows", each [Group]=k[Group] and [Created On]=k[Min Created]){0}[Value Entered], type date)

 

You wanted the first listed date in the group. The code filters the previous result to return all rows in the same group and then grabs the column value from the first row.

Thank you for the quick replies! - ill execute this once I get back to my desk.

 

Will keep you posted!

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.