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
shanwise101
New Member

finding the last record per day, per customer

Hello All,

I have a dataset that captures multiple customers who update a quantity field several times a day, differentiated by a timestamp.

I need to be able to display by way of table or column or measure:

  • the last record per day
  • per customer
  • and the associated quantity.

Dataset:

CustomertimestampQty
Customer A          4/04/2020 11:07          0
Customer A          4/04/2020 16:241
Customer A          4/04/2020 20:031
Customer A          5/04/2020 8:172
Customer A          5/04/2020 10:532
Customer A          5/04/2020 12:292
Customer A          5/04/2020 13:182
Customer A          5/04/2020 16:532
Customer A5/04/2020 19:593
Customer A          6/04/2020 8:513
Customer A          6/04/2020 9:561

 

Result (with additional customer for context)

CustomertimestampQty
Customer A          4/04/2020 20:03          1
Customer A5/04/2020 19:593
Customer A6/04/2020 9:561
Customer B6/04/2020 20:022
Customer B7/04/2020 7:552

 

Any guidance would be much appreciated, many thanks all.

3 ACCEPTED SOLUTIONS
mahoneypat
Employee
Employee

Here is one way to get your desired result as a DAX calculated table

 

1. Add a Date column to your table (I called your example data table "Qty") with this - 

Date = FORMAT(Qty[timestamp], "MM/DD/YYYY")
 
2. Make a DAX table with this expression

 

Latest =
ADDCOLUMNS (
    SUMMARIZE ( Qty, Qty[Customer], Qty[Date] ),
    "@time", CALCULATE ( MAX ( Qty[timestamp] ) ),
    "@Qty",
    VAR maxtime =
        CALCULATE ( MAX ( Qty[timestamp] ) )
    RETURN
        CALCULATE ( AVERAGE ( Qty[Qty] ), Qty[timestamp] = maxtime )
)

 

You could also use the above in a measure variable and do further analysis on it, if you prefer a measure.

 

If this works for you, please mark it as the solution.  Kudos are appreciated too.  Please let me know if not.

Regards,

Pat

 





Did I answer your question? Mark my post as a solution! Kudos are also appreciated!

To learn more about Power BI, follow me on Twitter or subscribe on YouTube.


@mahoneypa HoosierBI on YouTube


View solution in original post

camargos88
Community Champion
Community Champion

Hi @shanwise101 ,

 

Check this code for a calculated column:

 

Max_Timestamp = 
VAR _date = 'Table'[timestamp]
VAR _customer = 'Table'[Customer]
VAR _Qty = 'Table'[Qty]
RETURN
IF (_date = 
CALCULATE(
            MAX('Table'[timestamp]), 
            FILTER('Table', 
                    'Table'[Customer] = _customer && 
                    'Table'[Qty] = _Qty &&
                    DATE(YEAR('Table'[timestamp]), MONTH('Table'[timestamp]), DAY('Table'[timestamp])) = DATE(YEAR(_date), MONTH(_date), DAY(_date))
            )
), 1, 0)

 

Capture.PNG

 



Did I answer your question? Mark my post as a solution!

Proud to be a Super User!



View solution in original post

v-eachen-msft
Community Support
Community Support

Hi @shanwise101 ,

 

You could also use Power Query to do it.

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("ldA7CoAwDAbgq5TOhSbpS7OJxygdHUXwcX8rQtFF24zJlxD+GOV4bPsyT6sYRCmppNVgNQGBQGQIrxnIpGoWPZPNHazjBAzmj7vCO8aQG1SnEdiZBk5MfQM3jF0D923P9OyuZ8wX949gHNbrfNvfoacT", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Customer = _t, timestamp = _t, Qty = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Customer", type text}, {"timestamp", type datetime}, {"Qty", Int64.Type}}),
    #"Added Custom" = Table.AddColumn(#"Changed Type", "Custom", each Date.From([timestamp])),
    #"Grouped Rows" = Table.Group(#"Added Custom", {"Customer", "Custom"}, {{"Max", each _, type table [Customer=text, timestamp=datetime, Qty=number, Custom=date]}}),
    #"Removed Other Columns" = Table.SelectColumns(#"Grouped Rows",{"Max"}),
    #"Added Custom1" = Table.AddColumn(#"Removed Other Columns", "MaxDate", each List.Max([Max][timestamp])),
    #"Expanded Count" = Table.ExpandTableColumn(#"Added Custom1", "Max", {"Customer", "Qty", "timestamp"}, {"Max.Customer", "Max.Qty", "Max.timestamp"}),
    #"Filtered Rows" = Table.SelectRows(#"Expanded Count", each ([Max.timestamp] = [MaxDate])),
    #"Removed Columns" = Table.RemoveColumns(#"Filtered Rows",{"MaxDate"})
in
    #"Removed Columns"

Here is the result.

1-1.PNG

Here is the test file for your reference.

 

Community Support Team _ Eads
If this post helps, then please consider Accept it as the solution to help the other members find it.

View solution in original post

3 REPLIES 3
v-eachen-msft
Community Support
Community Support

Hi @shanwise101 ,

 

You could also use Power Query to do it.

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("ldA7CoAwDAbgq5TOhSbpS7OJxygdHUXwcX8rQtFF24zJlxD+GOV4bPsyT6sYRCmppNVgNQGBQGQIrxnIpGoWPZPNHazjBAzmj7vCO8aQG1SnEdiZBk5MfQM3jF0D923P9OyuZ8wX949gHNbrfNvfoacT", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Customer = _t, timestamp = _t, Qty = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Customer", type text}, {"timestamp", type datetime}, {"Qty", Int64.Type}}),
    #"Added Custom" = Table.AddColumn(#"Changed Type", "Custom", each Date.From([timestamp])),
    #"Grouped Rows" = Table.Group(#"Added Custom", {"Customer", "Custom"}, {{"Max", each _, type table [Customer=text, timestamp=datetime, Qty=number, Custom=date]}}),
    #"Removed Other Columns" = Table.SelectColumns(#"Grouped Rows",{"Max"}),
    #"Added Custom1" = Table.AddColumn(#"Removed Other Columns", "MaxDate", each List.Max([Max][timestamp])),
    #"Expanded Count" = Table.ExpandTableColumn(#"Added Custom1", "Max", {"Customer", "Qty", "timestamp"}, {"Max.Customer", "Max.Qty", "Max.timestamp"}),
    #"Filtered Rows" = Table.SelectRows(#"Expanded Count", each ([Max.timestamp] = [MaxDate])),
    #"Removed Columns" = Table.RemoveColumns(#"Filtered Rows",{"MaxDate"})
in
    #"Removed Columns"

Here is the result.

1-1.PNG

Here is the test file for your reference.

 

Community Support Team _ Eads
If this post helps, then please consider Accept it as the solution to help the other members find it.
camargos88
Community Champion
Community Champion

Hi @shanwise101 ,

 

Check this code for a calculated column:

 

Max_Timestamp = 
VAR _date = 'Table'[timestamp]
VAR _customer = 'Table'[Customer]
VAR _Qty = 'Table'[Qty]
RETURN
IF (_date = 
CALCULATE(
            MAX('Table'[timestamp]), 
            FILTER('Table', 
                    'Table'[Customer] = _customer && 
                    'Table'[Qty] = _Qty &&
                    DATE(YEAR('Table'[timestamp]), MONTH('Table'[timestamp]), DAY('Table'[timestamp])) = DATE(YEAR(_date), MONTH(_date), DAY(_date))
            )
), 1, 0)

 

Capture.PNG

 



Did I answer your question? Mark my post as a solution!

Proud to be a Super User!



mahoneypat
Employee
Employee

Here is one way to get your desired result as a DAX calculated table

 

1. Add a Date column to your table (I called your example data table "Qty") with this - 

Date = FORMAT(Qty[timestamp], "MM/DD/YYYY")
 
2. Make a DAX table with this expression

 

Latest =
ADDCOLUMNS (
    SUMMARIZE ( Qty, Qty[Customer], Qty[Date] ),
    "@time", CALCULATE ( MAX ( Qty[timestamp] ) ),
    "@Qty",
    VAR maxtime =
        CALCULATE ( MAX ( Qty[timestamp] ) )
    RETURN
        CALCULATE ( AVERAGE ( Qty[Qty] ), Qty[timestamp] = maxtime )
)

 

You could also use the above in a measure variable and do further analysis on it, if you prefer a measure.

 

If this works for you, please mark it as the solution.  Kudos are appreciated too.  Please let me know if not.

Regards,

Pat

 





Did I answer your question? Mark my post as a solution! Kudos are also appreciated!

To learn more about Power BI, follow me on Twitter or subscribe on YouTube.


@mahoneypa HoosierBI on YouTube


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.