Calculate the difference between two rows in Power…


Scenario: 

Staff need to calculate the difference of the number of products between every morning and every afternoon.

Table used: 

The table shows employees, products, periods, and the number of products on different days. If the employee’s corresponding product is missing a morning or an afternoon row, the default will be 0 products for the corresponding time period over three days.

yingyinr_0-1634782626034.png

Expected Result:

yingyinr_1-1634783960167.png

 

Solution 1:

1. Right-click on the ‘Queries’ panel and create a blank query.

yingyinr_5-1634784725753.png

2. In the advanced editor, enter the following query statement.

yingyinr_6-1634784725755.png

yingyinr_7-1634784865312.png

yingyinr_8-1634784865315.png

3. Go back to the main table and insert a new step in the applied steps. Then enter the statement at the underlined line.

yingyinr_9-1634785022509.png

yingyinr_10-1634785022511.png

4. Expand the columns in the tables, and it is unnecessary to check ‘Use original column name as prefix’.

yingyinr_11-1634785022513.png

yingyinr_12-1634785022517.png

 

Solution 2:

1. Group the tables according to the Employee column and the Product column and generate tables containing the Period column and Days column in the group.

yingyinr_13-1634785093621.png2. Transpose the Table column

yingyinr_14-1634785093624.png

yingyinr_15-1634785093625.png

3. Put the second row into the column name and fill in the missing columns.

yingyinr_16-1634785093628.png

yingyinr_17-1634785093629.png

4. Add a column to calculate the difference

yingyinr_18-1634785179834.png

yingyinr_19-1634785179837.png

5. Transpose the new custom column

yingyinr_20-1634785179838.png

yingyinr_21-1634785179840.png

6. Remove the columns we do not need and expand the columns we need.

yingyinr_22-1634785179843.png

7. Here is the final step to modify the column name.

yingyinr_23-1634785179846.png

 

Summary:

Comparing solution 1 with 2, solution 2 is more intuitive, because in addition to calculating the difference, it also completes the missing rows (e.g. some employees in the data source correspond to products missing the morning or afternoon rows).

 

Author: Stephen Tao 

Reviewer:  Kerry & Ula

Calculate the difference between two rows in Power Query.pbix



Source link

Be the first to comment

Leave a Reply

Your email address will not be published.


*