简体   繁体   中英

Merging multiple rows based on condition in Power BI

I have a table that looks like the one below.

Table I have

在此处输入图像描述

I want an output classifying the PART as either CLASS A or B. CLASS A and CLASS B should be created as two additional columns.

The conditions for classifications are:

-> IF Arrangement, Prefix and Range are the same, it will be classified as CLASS A and CLASS B, and the PART should be in both columns.

-> If the above condition is false, it will check the value in the TYPE column. If the type column contains CLASS A, then the respective PART should be in the CLASS A column. If the type column contains CLASS B, then the separate PART should be in the CLASS B column.

Here is the sample of the output I would need.

Table I want

在此处输入图像描述

So far, I have managed to get the below output but have not been able to merge the results into one row when the three columns are the same.

Results I got till now

到现在为止我得到的结果

Sample Data

Arrangements    PREFIX  RANGE   PART    TYPE
ARR1    1XJ 1-100   191123  TRANSMISSION CLASS A
ARR1    1XJ 1-100   299123  TRANSMISSION CLASS B
ARR1    9TC 1-100   191123  TRANSMISSION CLASS A
ARR2    5TJ 101-120 288123  TRANSMISSION CLASS B

Try this in M Advanced Editor

Table.AddColumn(previousStepName, "Text After Delimiter", each Text.AfterDelimiter([TYPE], " "), type text)

In the Query Editor (Transform Data), you can

  • Group by Arrangements , Prefix and Range and use the ALL operation

  • Then add Custom Columns with the formula below for CLASS A

    =try Table.SelectRows([Count], each Text.Contains([TYPE],"CLASS A"))[PART]{0} otherwise null

在此处输入图像描述

  • Add another Custom Column for CLASS B with the obvious changes
  • Delete the Count column

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM