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
markdem
Helper I
Helper I

Data from 2 columns based on 3rd column

Hello,
I have no idea how to word this question to google so sorry if it is something that has been answered before.


I have a simple time clock table I would like to report on. Table of events looks like this;

before.png

How would I go about making a new table that looks like this?

after.png

 

Thanks

1 ACCEPTED SOLUTION

hi  @markdem 

For your problem, it is because you have other columns that needs to be aggregated before pivot.

1.JPG

 

So adjust it as below:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("nVW7bhsxEPwX1dZin1ySrZAiiB03TGW4SpkA+f8uS0qibTl2jneVDicMZuex+/R0IOXD3eHhy/3p/vH0DSleTr///Pz19Xv8YmQ8YjqyN/SKWtkhF+xPfMXD893HAI8/2lsEsspWMQPZFcHi7wPD0gYSuWGulqoUYLoh8QHADYncKPcxAsGTX0loXiBRGmG1QBHI6ntIlMZUTaooWJILQjL7VM03JAS7HSyVQom8TYlXAH5EGgC5EgKrLPt5RZBUzQAnBeftSgyMyEQooQaGMjORtioREDwG0aoFRGSHEtxDpbmKg/t6qAZChCooGIPynMI/n+JfGNKzCUQzEy4LSkjDqAZXIrDbQbaRkEapklZMUfI5SOYFEtpJWAQjg7HvIaGNPNysYsAzE5xWLLWRiRLJioLtstSGpaXX4yWYycoCidSIo+IVORq2vicGAvNlYyabdhRasMMvJRUEvd0T2+zwnomoaIxBc/ELrVQ090yEo5QA0zYl3gFERbnnkjYC3EyR+xQSUhbIUwf9T73eY8S+9EpRc7yyYF9pR2lYgkO0FM6GrtpBOEpOlQmEriScFzJB1NUMEqbgtmf1BwKN66MxxrzDJLzdUjpv3RLxfnUDVywNBDpvGgWcdmjefMoDQjqJuEAaYuZdJMbG7NcnQZmbRnPsiee/", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [UserID = _t, UserName = _t, Type = _t, Datetime = _t, OtherColumn1 = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"UserID", Int64.Type}, {"UserName", type text}, {"Type", type text}, {"Datetime", type datetime}, {"OtherColumn1", Int64.Type}}),
    #"Inserted Date" = Table.AddColumn(#"Changed Type", "Date", each DateTime.Date([Datetime]), type date),
    #"Grouped Rows" = Table.Group(#"Inserted Date", {"UserID", "UserName", "Date"}, {{"New Othercolumn1", each List.Sum([OtherColumn1]), type number}}),
    #"Merged Queries" = Table.NestedJoin(#"Grouped Rows", {"UserID", "UserName", "Date"}, #"Inserted Date", {"UserID", "UserName", "Date"}, "Grouped Rows", JoinKind.Inner),
    #"Expanded Grouped Rows" = Table.ExpandTableColumn(#"Merged Queries", "Grouped Rows", {"Type", "Datetime"}, {"Type", "Datetime"}),
    #"Pivoted Column" = Table.Pivot(#"Expanded Grouped Rows", List.Distinct(#"Expanded Grouped Rows"[Type]), "Type", "Datetime")
in
    #"Pivoted Column"

 

here is sample pbix file, please try it.

 

Regards,

Lin

Community Support Team _ Lin
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

12 REPLIES 12
markdem
Helper I
Helper I

I now have one more issue. The "User” column needs to be joint with data from a different table.

If I try to merge the data with “= Table.NestedJoin(dbo_Events, {"userID"}, Users, {"userID"}, "Users", JoinKind.LeftOuter)” I get a error about not been able to have nested data in a pivot.

How can I get around this?

 

Thanks

v-lili6-msft
Community Support
Community Support

hi  @markdem 

For your case, you could try this way:

Step1:

The most important step is you need to add a date column for the datetime column as a bin.

You could selected datetime column and click Add column->Date->Date only as below to get it.

1.JPG

 

Step2:

Then selected clockin/out column and use Pivot it as below:

2.JPG

 

https://radacad.com/pivot-and-unpivot-with-power-bi

 

Step3:

Remove unless columns and get your expected output:

3.JPG

 

here is sample pbix file, please try it.

 

Regards,

lin

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

Sorry for the hugely delayed reply..
I have tried the instructions above but it only seems to be working when the clockin an clockout times are the same minute...

Here is my data now. Anyway I can get rid of all the nulls?

data.jpg

hi  @markdem 

This means that there is no ClockIN or ClockOUT for that date (there is only one of ClockIN or ClockOUT for that date), 

So you could filter null value for ClockIN and ClockOUT column

13.JPG

 

Regards,

Lin

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

But there is a event for both a IN and OUT.
If we have a look at user 157 (first column), I generated a clockIN event at 0800 and OUT event at 1700 on the dates between 21/06 and 28/06.

Thanks

Hi,

Share sample data in a format that can be pasted in an MS Excel file.


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/

Here is some sample data for 2 users.

142MELCLOCK01ClockIN2020-06-27T07:04:27.89000000
142MELCLOCK01ClockOUT2020-06-27T15:25:08.1500000501
156MELCLOCK01ClockIN2020-06-28T08:56:39.21000000
156MELCLOCK01ClockOUT2020-06-28T18:04:39.7670000548
156MELCLOCK01ClockIN2020-06-29T10:58:03.84700000
156MELCLOCK01ClockOUT2020-06-29T21:53:34.5630000655
142MELCLOCK01ClockIN2020-06-30T07:23:19.28000000
156MELCLOCK01ClockIN2020-07-01T07:28:10.24300000
142MELCLOCK01ClockOUT2020-07-01T07:36:55.0800000720
156MELCLOCK01ClockOUT2020-07-01T15:53:45.5030000506
142MELCLOCK01ClockIN2020-07-02T07:24:49.33300000
156MELCLOCK01ClockIN2020-07-02T08:48:37.77000000
156MELCLOCK01ClockOUT2020-07-02T18:24:52.4230000576
142MELCLOCK01ClockOUT2020-07-02T18:38:04.1130000673
142MELCLOCK01ClockIN2020-07-03T06:32:11.57000000
142MELCLOCK01ClockOUT2020-07-03T16:14:06.8930000582
142MELCLOCK01ClockIN2020-07-04T06:57:38.52700000
142MELCLOCK01ClockOUT2020-07-04T17:49:35.2030000260
156MELCLOCK01ClockIN2020-07-05T07:29:53.56000000
156MELCLOCK01ClockOUT2020-07-05T18:29:10.5030000659
156MELCLOCK01ClockIN2020-07-06T12:34:02.24700000
156MELCLOCK01ClockOUT2020-07-06T22:25:08.6530000591
142MELCLOCK01ClockIN2020-07-07T07:36:30.48000000
142MELCLOCK01ClockOUT2020-07-07T16:53:02.1900000316
142MELCLOCK01ClockIN2020-07-08T06:49:16.06700000
156MELCLOCK01ClockIN2020-07-08T08:42:06.16700000
156MELCLOCK01ClockOUT2020-07-08T16:35:09.8800000473
142MELCLOCK01ClockOUT2020-07-08T18:17:12.4070000272
142MELCLOCK01ClockIN2020-07-09T09:09:32.53000000
142MELCLOCK01ClockOUT2020-07-10T06:31:21.3170000721
142MELCLOCK01ClockIN2020-07-11T08:09:54.75300000
142MELCLOCK01ClockOUT2020-07-11T17:23:42.5100000132
156MELCLOCK01ClockIN2020-07-12T08:49:25.56300000
156MELCLOCK01ClockOUT2020-07-12T16:57:34.0070000488
156MELCLOCK01ClockIN2020-07-13T08:15:41.38300000
156MELCLOCK01ClockOUT2020-07-13T16:24:46.9600000489

 

Thanks

Hi,

This M code works

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("nVW7bhsxEPwX1dZin1ySrZAiiB03TGW4SpkA+f8uS0qibTl2jneVDicMZuex+/R0IOXD3eHhy/3p/vH0DSleTr///Pz19Xv8YmQ8YjqyN/SKWtkhF+xPfMXD893HAI8/2lsEsspWMQPZFcHi7wPD0gYSuWGulqoUYLoh8QHADYncKPcxAsGTX0loXiBRGmG1QBHI6ntIlMZUTaooWJILQjL7VM03JAS7HSyVQom8TYlXAH5EGgC5EgKrLPt5RZBUzQAnBeftSgyMyEQooQaGMjORtioREDwG0aoFRGSHEtxDpbmKg/t6qAZChCooGIPynMI/n+JfGNKzCUQzEy4LSkjDqAZXIrDbQbaRkEapklZMUfI5SOYFEtpJWAQjg7HvIaGNPNysYsAzE5xWLLWRiRLJioLtstSGpaXX4yWYycoCidSIo+IVORq2vicGAvNlYyabdhRasMMvJRUEvd0T2+zwnomoaIxBc/ELrVQ090yEo5QA0zYl3gFERbnnkjYC3EyR+xQSUhbIUwf9T73eY8S+9EpRc7yyYF9pR2lYgkO0FM6GrtpBOEpOlQmEriScFzJB1NUMEqbgtmf1BwKN66MxxrzDJLzdUjpv3RLxfnUDVywNBDpvGgWcdmjefMoDQjqJuEAaYuZdJMbG7NcnQZmbRnPsiee/", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"User ID" = _t, #"User Name" = _t, Type = _t, #"Date/Time" = _t, Value = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"User ID", Int64.Type}, {"User Name", type text}, {"Type", type text}, {"Date/Time", type datetime}, {"Value", Int64.Type}}),
    #"Duplicated Column" = Table.DuplicateColumn(#"Changed Type", "Date/Time", "Date/Time - Copy"),
    #"Changed Type1" = Table.TransformColumnTypes(#"Duplicated Column",{{"Date/Time - Copy", type date}}),
    #"Removed Columns" = Table.RemoveColumns(#"Changed Type1",{"Value"}),
    #"Pivoted Column" = Table.Pivot(#"Removed Columns", List.Distinct(#"Removed Columns"[Type]), "Type", "Date/Time"),
    #"Renamed Columns" = Table.RenameColumns(#"Pivoted Column",{{"Date/Time - Copy", "Date"}})
in
    #"Renamed Columns"

Hope this helps.

Untitled.png


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/

hi  @markdem 

For your problem, it is because you have other columns that needs to be aggregated before pivot.

1.JPG

 

So adjust it as below:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("nVW7bhsxEPwX1dZin1ySrZAiiB03TGW4SpkA+f8uS0qibTl2jneVDicMZuex+/R0IOXD3eHhy/3p/vH0DSleTr///Pz19Xv8YmQ8YjqyN/SKWtkhF+xPfMXD893HAI8/2lsEsspWMQPZFcHi7wPD0gYSuWGulqoUYLoh8QHADYncKPcxAsGTX0loXiBRGmG1QBHI6ntIlMZUTaooWJILQjL7VM03JAS7HSyVQom8TYlXAH5EGgC5EgKrLPt5RZBUzQAnBeftSgyMyEQooQaGMjORtioREDwG0aoFRGSHEtxDpbmKg/t6qAZChCooGIPynMI/n+JfGNKzCUQzEy4LSkjDqAZXIrDbQbaRkEapklZMUfI5SOYFEtpJWAQjg7HvIaGNPNysYsAzE5xWLLWRiRLJioLtstSGpaXX4yWYycoCidSIo+IVORq2vicGAvNlYyabdhRasMMvJRUEvd0T2+zwnomoaIxBc/ELrVQ090yEo5QA0zYl3gFERbnnkjYC3EyR+xQSUhbIUwf9T73eY8S+9EpRc7yyYF9pR2lYgkO0FM6GrtpBOEpOlQmEriScFzJB1NUMEqbgtmf1BwKN66MxxrzDJLzdUjpv3RLxfnUDVywNBDpvGgWcdmjefMoDQjqJuEAaYuZdJMbG7NcnQZmbRnPsiee/", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [UserID = _t, UserName = _t, Type = _t, Datetime = _t, OtherColumn1 = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"UserID", Int64.Type}, {"UserName", type text}, {"Type", type text}, {"Datetime", type datetime}, {"OtherColumn1", Int64.Type}}),
    #"Inserted Date" = Table.AddColumn(#"Changed Type", "Date", each DateTime.Date([Datetime]), type date),
    #"Grouped Rows" = Table.Group(#"Inserted Date", {"UserID", "UserName", "Date"}, {{"New Othercolumn1", each List.Sum([OtherColumn1]), type number}}),
    #"Merged Queries" = Table.NestedJoin(#"Grouped Rows", {"UserID", "UserName", "Date"}, #"Inserted Date", {"UserID", "UserName", "Date"}, "Grouped Rows", JoinKind.Inner),
    #"Expanded Grouped Rows" = Table.ExpandTableColumn(#"Merged Queries", "Grouped Rows", {"Type", "Datetime"}, {"Type", "Datetime"}),
    #"Pivoted Column" = Table.Pivot(#"Expanded Grouped Rows", List.Distinct(#"Expanded Grouped Rows"[Type]), "Type", "Datetime")
in
    #"Pivoted Column"

 

here is sample pbix file, please try it.

 

Regards,

Lin

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

Perfect. I did not know abou this.

Thanks

Anonymous
Not applicable

@markdem ,

 

You could achieve this by unpoviting your tabel by clockin/out column.

 

https://support.microsoft.com/en-us/office/unpivot-columns-power-query-0f7bad4b-9ea1-49c1-9d95-f5882...

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.