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
GavinR87
Helper II
Helper II

New columns from row data

Hi community,

 

Slightly more complex one that I am hoping someone can help with. Please bare with me and hopefully this makes some sort of sense!

 

We receive traffic data in CSV format.

 

The important columns of this data is date, time (minute averages), detector address, type, and value.

 

Now type can be one of a few things, the most common being either speed or flow. The value cell therefore is either the value of speed or flow, depending on what the adjacent "Type" column says.

 

Hope this makes sense so far.

 

So we have something a bit like this:

 

DateTimeDetectorTypeValue
1/1/2000:011234Speed80
1/1/2000:014567Speed78
1/1/2000:011234Flow20
1/1/2000:014567Flow21

 

What I am looking to do is make new columns for speed and flow, and then lose the redundant rows, so my above example would end up like this:

 

DateTimeDetectorSpeedFlow
1/1/2000:0112348020
1/1/2000:0145677821

 

As you can see I now have columns for speed and flow, and these values are now in single rows, which halves the amount of rows I had previously.

 

Now in Excel I could do this with a few ifs and get the result I want fairly easy. But I want to avoid having to manually manipulate the multiple CSV files per day, so would like to do this in Power BI somehow.

 

I am sure there must be a way.

 

As always grateful for any guidance here!

 

Cheers

1 ACCEPTED SOLUTION
Anonymous
Not applicable

Here's all the code you need (insert it into a blank query in PQ and execute):

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMtQ31DcyUNJRMjCwMjAE0oZGxiZAKrggNTUFSFsYKMXqYFFmYmpmjqTM3AK7Mqhpbjn55UDKCL9hMFWGSrGxAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [Date = _t, Time = _t, Detector = _t, Type = _t, Value = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Date", type date}, {"Time", type time}, {"Detector", Int64.Type}, {"Type", type text}, {"Value", Int64.Type}}),
    #"Pivoted Column" = Table.Pivot(#"Changed Type", List.Distinct(#"Changed Type"[Type]), "Type", "Value")
in
    #"Pivoted Column"

 

Best

D

View solution in original post

4 REPLIES 4
Anonymous
Not applicable

In Power Query you can get this even easier than in Excel. As already suggested - pivot the relevant columns. It's a one-click operation in PQ.

Best
D
Anonymous
Not applicable

Here's all the code you need (insert it into a blank query in PQ and execute):

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMtQ31DcyUNJRMjCwMjAE0oZGxiZAKrggNTUFSFsYKMXqYFFmYmpmjqTM3AK7Mqhpbjn55UDKCL9hMFWGSrGxAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [Date = _t, Time = _t, Detector = _t, Type = _t, Value = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Date", type date}, {"Time", type time}, {"Detector", Int64.Type}, {"Type", type text}, {"Value", Int64.Type}}),
    #"Pivoted Column" = Table.Pivot(#"Changed Type", List.Distinct(#"Changed Type"[Type]), "Type", "Value")
in
    #"Pivoted Column"

 

Best

D

Hi @GavinR87 ,

 

pivot the columns

https://docs.microsoft.com/en-us/power-bi/desktop-common-query-tasks#pivot-columns

 

Did I answer your question?
Please mark my post as solution, this will also help others.
Please give Kudos for support.

Marcus Wegener works as Full Stack Power BI Engineer at BI or DIE.
His mission is clear: "Get the most out of data, with Power BI."
twitter - LinkedIn - YouTube - website - podcast


Hi @mwegener 

 

Thanks so much for the reply.

 

I did the pivot and it sort of worked, but still not quite what I want. I more than likely did something a bit wrong!

 

So now I've gone from this:

 

DateTimeDetectorTypeValue
1/1/2000:011234Speed80
1/1/2000:014567Speed78
1/1/2000:011234Flow20
1/1/2000:014567Flow21

 

To:

 

DateTimeDetectorSpeedFlow
1/1/2000:01123480Null
1/1/2000:01456778Null
1/1/2000:011234Null20
1/1/2000:014567Null21

 

Is there an easy way to get from the above to:

 

DateTimeDetectorSpeedFlow
1/1/2000:0112348020
1/1/2000:0145677821

 

Hope this makes sense, and again appreciate your time for answering 🙂

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.