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
PBI_newuser
Post Prodigy
Post Prodigy

How to Pivot the table in query to get one ID with different dates?

Hi, 

I would like to pivot the data to have below output (One ID with different date fields).

How to do this? here

Sample data here.

 

IDcreatedOrder Status - ClosedOrder - DispatchedField Status - AcceptedField Status - Closed.............
ID-0127504912/31/2019 4:01:04 PM2/18/2020 2:59:44 PM   xxxxx
ID-0127505212/31/2019 4:01:11 PM 12/31/2019 4:03:28 PM12/31/2019 4:11:36 PM12/31/2019 4:12:00 PMxxxxx
ID-0127510612/31/2019 4:08:23 PM 12/31/2019 4:08:52 PM1/8/2020 4:53:52 PM1/8/2020 8:05:03 PMxxxx
ID-0127506912/31/2019 4:02:58 PM12/31/2019 4:42:08 PM   xxxxx


PBI_newuser_1-1611734439361.png

 

 

1 ACCEPTED SOLUTION

Hi, @PBI_newuser 

 

I modified the measure and column to make the result correct. In my original logic, each value will be repeated twice, so your result will be doubled. 

I found that 'created' value doesn't appear after January 2020, so my results actually only have valid values for two months.

The mmmyy column I made is only used to format the date of the line chart, and cannot be used for filtering, because your table data is too large to pvoit and it will filter different value values.

Column1 = 
VAR a = data[ID]
VAR b =
    MAXX (
        FILTER ( data, [ID] = a && [History New Value] = "created" ),
        [History CreatedDate]
    )
VAR c =
    MAXX (
        FILTER ( data, [ID] = a && [History New Value] = "Field Status - Accepted" ),
        [History CreatedDate]
    )
RETURN 
    IF(b<>BLANK()&&c<>BLANK(),
    DIVIDE ( DATEDIFF ( b, c, SECOND ), 3600 )
)
Measure1 = 
VAR a =
    CALCULATE ( COUNT ( data[Column1] ), FILTER ( data, [Column1] <> BLANK ()&&[History New Value]="created" ) )
    var b= 
CALCULATE ( SUM( data[Column1] ), FILTER ( data, [Column1] <> BLANK ()&&[History New Value]="created" ) )
RETURN
    DIVIDE ( b, a * 2 )

6.png

Here is my sample .pbix file. Hope you can understand what I said.

If it doesn’t solve your problem, please feel free to ask me.

 

Best Regards

Janey Guo

 

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

16 REPLIES 16
v-janeyg-msft
Community Support
Community Support

Hi, @PBI_newuser 

 

You can use 'pivot column' function in PQ.

Like this:

1.gif

Best Regards

Janey Guo

 

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

Hi @v-janeyg-msft , i tried the solution you proposed, but maybe data is too large, so it takes long time to load and ended up i got the below error message.

DataSource.Error: The operation has timed out

 

Is there any other way (e.g. DAX column) to create the date columns?

Hi, @PBI_newuser 

 

If you data is large, using pq will time out, then using dax is estimated to be similar, you can use a matrix visual to show the pviot table.

Like this:

3.png

If it doesn’t solve your problem, please feel free to ask me.

 

Best Regards

Janey Guo

 

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

Hi @v-janeyg-msft , thanks! It works.

But, I need more columns to show beside the date columns.

How can I create columns to show the Hours difference between

1) [created] and [Order Status-Dispatched], 

2) [Field Status - For Dispatch] and [Order - Dispatched], etc.?

Hi, @PBI_newuser  What kind of result graph do you want?

Hi @v-janeyg-msft ,

I wanna calculate the hours difference between the columns first and display as the screenshot, then plot a chart with the average of the hours difference by month. Is it possible?

PBI_newuser_0-1612249398359.png

 

Hi, @PBI_newuser 

 

I create some measures and columns to display your desired result.

Like this:

Column1 = 
VAR a = data[ID]
VAR b =
    MAXX (
        FILTER ( data, [ID] = a && [History New Value] = "created" ),
        [History CreatedDate]
    )
VAR c =
    MAXX (
        FILTER ( data, [ID] = a && [History New Value] = "Field Status - Accepted" ),
        [History CreatedDate]
    )
RETURN
    DIVIDE ( DATEDIFF ( b, c, SECOND ), 3600 )
Measure1 = 
VAR a =
    CALCULATE ( COUNT ( data[Column1] ), FILTER ( data, [Column1] <> BLANK () ) )
RETURN
    DIVIDE ( SUM ( data[Column1] ), a * 2 )

4.png

Here is my sample .pbix file.Hope it helps.

If it doesn’t solve your problem, please feel free to ask me.

 

Best Regards

Janey Guo

 

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

Hi @v-janeyg-msft , thank you so much for your help!

  • The measure is not working correctly. I tried to download Dec-19 data to calculate the average hours. The average hours for Measure1 in Dec-19 should be 254.9 while for Measure2 is 363.7.
  • For the period "mmm-yy", can we use the "created" column instead of "History CreatedDate"? But "created" column in the table is not a separate column. 

PBI_newuser_3-1612276213974.png

 

PBI_newuser_4-1612276343060.png

 

PBI_newuser_1-1612275948734.png

Thank you!

Hi, @PBI_newuser 

 

I modified the measure and column to make the result correct. In my original logic, each value will be repeated twice, so your result will be doubled. 

I found that 'created' value doesn't appear after January 2020, so my results actually only have valid values for two months.

The mmmyy column I made is only used to format the date of the line chart, and cannot be used for filtering, because your table data is too large to pvoit and it will filter different value values.

Column1 = 
VAR a = data[ID]
VAR b =
    MAXX (
        FILTER ( data, [ID] = a && [History New Value] = "created" ),
        [History CreatedDate]
    )
VAR c =
    MAXX (
        FILTER ( data, [ID] = a && [History New Value] = "Field Status - Accepted" ),
        [History CreatedDate]
    )
RETURN 
    IF(b<>BLANK()&&c<>BLANK(),
    DIVIDE ( DATEDIFF ( b, c, SECOND ), 3600 )
)
Measure1 = 
VAR a =
    CALCULATE ( COUNT ( data[Column1] ), FILTER ( data, [Column1] <> BLANK ()&&[History New Value]="created" ) )
    var b= 
CALCULATE ( SUM( data[Column1] ), FILTER ( data, [Column1] <> BLANK ()&&[History New Value]="created" ) )
RETURN
    DIVIDE ( b, a * 2 )

6.png

Here is my sample .pbix file. Hope you can understand what I said.

If it doesn’t solve your problem, please feel free to ask me.

 

Best Regards

Janey Guo

 

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

Hi @v-janeyg-msft , I got what you meant but each value doesn't repeat twice but some more than twice. I have created 2 new pages with new dataset (only Oct-20 data) here. I downloaded the data from "Sample1" page, and noticed same ID has repeated more than twice.

 

PBI_newuser_6-1612324590873.png

The "Sample2" page should got the below result (in "Datachecking" file) but I am not sure how to calculate it by using the measures.

PBI_newuser_5-1612324585718.png

Thanks!

Hi, @PBI_newuser 

 

There is no duplication in the matrix, because there are row and column restrictions in the matrix. I have restricted all measures and columns, and will not calculate values that should not be calculated. Can you understand? If you want to display in the table, you need to filter in filterpane

Please carefully look at the measure and column I wrote, and understand the context in each visual!

Best Regards

Janey Guo

 

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

Hi @v-janeyg-msft , thank you so much for the time and effort you put toward helping me find the right solution for my report! Truly appreciate it! 😊

Hi, @PBI_newuser 

 

If you can understand the role of these formulas in visuals, I will be happy. This is not the best practice, because your data volume is too large to be transposed, so the logic considered will be different. And I will be happier if you can give me some kudos.😊

 

Best Regards

Janey Guo

Hi @v-janeyg-msft , yes, my data volume is too large, even refresh a table takes few hours to complete. I need more time to understand the formulas as I am still new to Power BI. Sure I can give you some kudos as you are very supportive! Your detailed explanation helps me a lot! Thanks again! 😀

amitchandak
Super User
Super User

@PBI_newuser , select the other column then id and unpivot in power query

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

Hi @amitchandak , it's not working.

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.