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
nileshwh
Frequent Visitor

Matrix shows columns in ascending but not the order as per the power query

Hi 

 

I want to create the report based on below Multiple SharePoint List data.

SharePoint Lists  
Dept IDDept Name 
1IT 
2HR 
3Finance 
EmployeeDetails:  
Employee IDEmployee NameDept ID
100John1
200Linda2
101Steve1
201Ramesh2
102Raj1

 

The report should be displayed in below format.

Dept IDDept NameEmployee IDEmployee NameEmployee IDEmployee NameEmployee IDEmployee Name
1IT100John101Steve102Raj
2HR200Linda201Ramesh  

 

I have written the below Power Query using Power BI Desktop:

let
Source = Table.NestedJoin(DeptDetails, {"ID"}, EmpDetails, {"DeptID.lookupId"}, "EmpDetails", JoinKind.LeftOuter),
#"Expanded EmpDetails" = Table.ExpandTableColumn(Source, "EmpDetails", {"EmpID", "EmployeeName"}, {"EmpDetails.EmpID", "EmpDetails.EmployeeName"}),
#"Grouped Rows" = Table.Group(#"Expanded EmpDetails", {"DeptID"}, {{"AllRows", each Table.AddIndexColumn(_, "Index", 0, 1), type table}}),
#"Expanded AllRows" = Table.ExpandTableColumn(#"Grouped Rows", "AllRows", {"DeptName", "EmpDetails.EmpID", "EmpDetails.EmployeeName", "Index"}, {"AllRows.DeptName", "AllRows.EmpDetails.EmpID", "AllRows.EmpDetails.EmployeeName", "AllRows.Index"}),
IndexToText = Table.TransformColumns(#"Expanded AllRows",{{"AllRows.Index", each if _ = 0 then "" else Text.From(_), type text}}),
#"Unpivoted Other Columns" = Table.UnpivotOtherColumns(IndexToText, {"DeptID", "AllRows.Index"}, "Attribute", "Value"),
#"Merged Columns" = Table.CombineColumns(Table.TransformColumnTypes(#"Unpivoted Other Columns", {{"AllRows.Index", type text}}, "en-US"),{"Attribute", "AllRows.Index"},Combiner.CombineTextByDelimiter("", QuoteStyle.None),"Merged")
in
#"Merged Columns"

 

The output of this query is as below:

nileshwh_0-1632379337528.png

The merge column is having column names in sequence  i.e 1. DeptName, EmpID and then EmpName.

 

I used the Matrix visual in visualization and assign DeptID as Rows, Merged as Columns and Value as Value in Matrix

 

But Matrix shows the column in ascending order like All  deptName, then all EmpID and then All EmpName.

nileshwh_1-1632379767758.png

 

But I need to display the data as DeptName, EmpID, EmpName, DeptName1, EmpID1, EmployeeName1, DeptName2, EmpID2, EmployeeName2.

Please let me know where I can change column sequence

 

Regards,

Nilesh

 

16 REPLIES 16
Greg_Deckler
Super User
Super User

@nileshwh Sort by the first thing you want then hold down the Shift key to select the other columns you want to sort by in sequence.


@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...

@Greg_Deckler ,

 

Thank you for your quick reply.

 

But I am not cleared about your solution. Can you elaborate it in detail?  Sorting in Matrix at column level? I could not found any option for columns. If you provide me any sample solution then it will help me a lot in undestanding.

@nileshwh I misunderstood and was thinking about a table. So where is AllRows.DeptName1, AllRows.DeptName2, AllRowsDeptName3 coming from?


@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...

@Greg_Deckler,

 

It is coming from Power query. Please check my first post which includes power query and its output as screenshot.

@nileshwh Are you married to that PQ or would something like this work?

Greg_Deckler_0-1632385252725.png

 


@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...

@Greg_Deckler 

I am fine with the output which is shown in screenshot. I will be more than happy with such outout. My actual requirement is what you have shown in screenshot. 

@nileshwh OK, see attached. Forget the PQ stuff and just leave them as two separate tables. See Table30 and Table30a in attached PBIX below sig. Page 6.


@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...

@Greg_Deckler ,

I really apologize for my delayed response and thank you for providing the solution. It is working as per our discussion.  

While implementing this solution, I am facing some formating issues. It is showing DeptName below DeptID. Both column should be displayed in same position. Please find the screenshot below with highlighted in yellow. I checked your file where it shows in proper format but here I am not able to arrange deptid and deptname in same line horizontally

nileshwh_0-1632472259310.png

 

Secondly there is one modification in my current requirement. Please find the details below:

The data is stored in Multiple SharePoint list as per the below screenshot. Here two new list has been added

1. EmpProject is child list of EmpDetails list. EmpProject stores all the project details related to each employee.

2. LocationDetails - This list store the information about the locations where each department is located. 

nileshwh_1-1632472834689.png

 

Now I want to generate the report which will show data in single row for each dept.

nileshwh_3-1632476795651.png

nileshwh_4-1632476861508.png

 

I have provided the link below to view my pbix file and also excel file which contain report format and data 

Pbix and Excel with Report Format and data 

 

Please help me to generate the report in required format.

I really appriciate your help.

 

 

@nileshwh For you first question, you need to go to the Format sub-pane of the Visualizations pane. Expand Row headers section and turn off Stepped layout. I'll have to look at the second request. I was not able to download the PBIX basically said "not found".


@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...

@Greg_Deckler , First issue is resolved(Formating the deptName Column).

 

Attachment option is not enable for my id in this forum so could not attached the files here. Is there any other way I can attach the files?

I am reattaching file links below:

Report file 

Pbix File 

 

 

Hi @nileshwh,

It sounds like you want to achieve custom sorting orders on your table records.
For your scenario, I'd like to suggest you take a look at the following blog about create a custom sorting table and setting the 'sort by column' property to achieve your requirement.

Custom Sorting in Power BI 

Regards,

Xiaoxin Sheng

Community Support Team _ Xiaoxin
If this post helps, please consider accept as solution to help other members find it more quickly.

Hi @v-shex-msft ,

Thank you for your response. I tried the options you mentioned in your post. But could not achieve the desired output.

I have one query created using three tables - DeptDetails, EmpDetails and EmpProjects

1. Tables:

nileshwh_0-1632827726432.png

One More Table: MatchColumns - This table is used to joing with query to display the record in desired sequence.

2. Query:  Merge-Dept-Emp-Projects

nileshwh_1-1632827973627.png

3. Visual : I added Matrix on the visualization page and assign rows, header, values as below:

 

nileshwh_2-1632828567947.png

But now Issue is it shows only 5 columns from the data.

Please refere  the Pbix file by using following linik

Index fo column Order 

 

Please help me to resolve this issue

HI @nileshwh,

You can extract the 'Merge' field values to a new table, add a custom column to check merge field values to setting priority number(deptName: 1, EmpID: 2, EmpName: 3), and config custom column sort by 'ASC'.
Then you can add an index column to the new table and setting the index as 'sort by column' property at data view side and build the relationship from this table to raw table.
After these steps, you can use new table merge field on your matrix and it will show as you wanted.

Regards,

Xiaoxin Sheng

Community Support Team _ Xiaoxin
If this post helps, please consider accept as solution to help other members find it more quickly.

Hi @v-shex-msft ,

 

Thank you for your reply.

I am not able to understand the solution you provided. Can you elaborate in details using my example provided in previous post? 

Please provide the details on below points

1. Merge field values in new table(how to create it and using which columns)

2. Custom column to setting priority numbers( How to create priority numbers in custom column)

3. setting the index as 'sort by column' property at data view side

4. build the relationship from this table to raw table

 

Really appreciate your help.

 

 

 

 

 

 

Hi @v-shex-msft ,

 

Waiting for your reply.

 

Regards,

HI @nileshwh,

I check your sample file but I can't do any custom on your pbix query edit side, these source data is invalid to view due to security reasons.

I create some dummy data as source data and build a sample pbix below, you can try it if helps:

1.png

Regards,

Xiaoxin Sheng

Community Support Team _ Xiaoxin
If this post helps, please consider accept as solution to help other members find it more quickly.

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.