Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Compete to become Power BI Data Viz World Champion! First round ends August 18th. Get started.

Reply
rocky09
Solution Sage
Solution Sage

Transpose Row Data into Column based on a criteria

I have this below table.  It contains some activities for a Ticket. A Ticket may contains many Activities and each activity has a unique ID.

 

ActivityIDDateInteractionIDTypeModifiedBy
221816324-02-2015 20:491388956EmailSteven
221816424-02-2015 20:501388956EmailSystem
221816525-02-2015 08:141388956EmailSamuel
221816625-02-2015 08:151388956EmailSystem

 

Is it possible to place the rows based on a criteria.

 

The criteria is, where the modifiedby is "System", then that particualr Activity should placed just above the Activity.  See below table, what i am trying to get it.

 

ActivityIDDateInteractionIDTypeModifiedByActivityIDDateInteractionIDTypeModifiedBy
221816324-02-2015 20:491388956EmailSteven221816424-02-2015 20:501388956EmailSystem
221816525-02-2015 08:141388956EmailSamuel221816625-02-2015 08:151388956EmailSystem

 

Any Help is appreciated.

 

Thank you,

1 ACCEPTED SOLUTION

Hi @rocky09,

 

You can write the T-SQL query like below:

 

SELECT
  *
FROM test0825 t 
CROSS APPLY (SELECT TOP 1
  ActivityID ActivityID_,
  Date Date_,
  InteractionID InteractionID_,
  Type Type_,
  ModifiedBy ModifiedBy_
FROM test0825
WHERE ModifiedBy = 'system'
AND InteractionID = t.InteractionID
AND Date >= t.Date
ORDER BY date ASC) ca
WHERE t.ModifiedBy <> 'system'

 

w6.PNG

 

If you have any question about T-SQL query, please post a thread in Transact-SQL forum.

 

Best Regards,
QiuyunYu

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

6 REPLIES 6
CahabaData
Memorable Member
Memorable Member

will System always be the next sequential record?

www.CahabaData.com

@CahabaData

 

Yes. System will be the next sequential record.

there's more than one way to skin this cat.  since System is always the next sequential plus you have a sequential Activity ID - it gives you alot of choices;

 

in a formula / measure solution one can employ the EARLIER function

 

or you can model it at the table level.  if the record set is not super huge I myself prefer the table level but it does use more memory resource.  One could duplicate tables with a filter on ModifiedBy so you have the System Table and the non System Table....  then in the System table create a new calculate column SysActivityID which is ActivityID less 1.   Then join these 2 tables together - that gives you all fields in 1 row at the table level.  As described here these are all steps in the Query Editor which will auto fire each time you refresh data.

www.CahabaData.com

@CahabaData

 

Good Idea. But, unfortunately, the database is huge and fetching from live sql server. Anyway, I will your suggestion and see the result.

 

Btw, can we do something in Sql query itself?

Hi @rocky09,

 

You can write the T-SQL query like below:

 

SELECT
  *
FROM test0825 t 
CROSS APPLY (SELECT TOP 1
  ActivityID ActivityID_,
  Date Date_,
  InteractionID InteractionID_,
  Type Type_,
  ModifiedBy ModifiedBy_
FROM test0825
WHERE ModifiedBy = 'system'
AND InteractionID = t.InteractionID
AND Date >= t.Date
ORDER BY date ASC) ca
WHERE t.ModifiedBy <> 'system'

 

w6.PNG

 

If you have any question about T-SQL query, please post a thread in Transact-SQL forum.

 

Best Regards,
QiuyunYu

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

@v-qiuyu-msft

Thank you so much.

Helpful resources

Announcements
July 2025 community update carousel

Fabric Community Update - July 2025

Find out what's new and trending in the Fabric community.

July PBI25 Carousel

Power BI Monthly Update - July 2025

Check out the July 2025 Power BI update to learn about new features.

Top Solution Authors