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
PedroAliança
Frequent Visitor

custom column to compare standard

Hello, everone. 

I use a lot of DAX to handle data and I'm gradually migrating to M. 

This time I'm trying to format all clients that start with the first 10 digits of the CNPJ with the same name...

In Dax I do it:

Spoiler

var comCNPJ =
MAXX(
FILTER(ALL('1.4 SQL Clientes'),
LEFT('1.4 SQL Clientes'[CNPJ_CPF], 10) = LEFT(EARLIER('1.4 SQL Clientes'[CNPJ_CPF]), 10)
), '1.4 SQL Clientes'[Nome]
)

RETURN
IF('1.4 SQL Clientes'[CNPJ_CPF] = BLANK(), '1.4 SQL Clientes'[Nome], comCNPJ)

Does anyone have any idea how to do this in power query?

Tank you!

1 ACCEPTED SOLUTION
v-stephen-msft
Community Support
Community Support

Hi @PedroAliança ,

 

You want the following calculations to be listed in Power Query, right?

vstephenmsft_2-1672730276179.png

Here's the solution in Power Query.

1.Add a custom column to return the first ten characters starting from the left.

vstephenmsft_0-1672734032795.png

2.Add an index column.

vstephenmsft_1-1672734085256.png

vstephenmsft_3-1672734127672.png

3.Group by the [LEFT10] and set up as follows.

vstephenmsft_2-1672734111264.png

vstephenmsft_4-1672734172380.png

4.Expand the All Rows column

vstephenmsft_5-1672734183772.png

5.Add a conditional column to compare.

vstephenmsft_6-1672734210862.png

6.Fill up the column.

vstephenmsft_7-1672734249677.png

vstephenmsft_8-1672734278341.png

 

7.Sort based on the index column and the [Custom] column is the result.

vstephenmsft_9-1672734297201.png

vstephenmsft_10-1672734311034.png

 

 

Best Regards,

Stephen Tao

 

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

7 REPLIES 7
v-stephen-msft
Community Support
Community Support

Hi @PedroAliança ,

 

You want the following calculations to be listed in Power Query, right?

vstephenmsft_2-1672730276179.png

Here's the solution in Power Query.

1.Add a custom column to return the first ten characters starting from the left.

vstephenmsft_0-1672734032795.png

2.Add an index column.

vstephenmsft_1-1672734085256.png

vstephenmsft_3-1672734127672.png

3.Group by the [LEFT10] and set up as follows.

vstephenmsft_2-1672734111264.png

vstephenmsft_4-1672734172380.png

4.Expand the All Rows column

vstephenmsft_5-1672734183772.png

5.Add a conditional column to compare.

vstephenmsft_6-1672734210862.png

6.Fill up the column.

vstephenmsft_7-1672734249677.png

vstephenmsft_8-1672734278341.png

 

7.Sort based on the index column and the [Custom] column is the result.

vstephenmsft_9-1672734297201.png

vstephenmsft_10-1672734311034.png

 

 

Best Regards,

Stephen Tao

 

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

Good Morning!

It worked just fine. Thanks for the class!

PedroAliança
Frequent Visitor

Can sameone help me?

PedroAliana_0-1671717179082.png

 

I suggest you post a usable example of data (either as text which can be copy/pasted or as a link to an uploaded workbook) as well as an example (could be a screenshot), of the results you expect from that same data. Obscure any confidential information, but ensure the data is representative.

wdx223_Daniel
Super User
Super User

=let fx=(t)=>Text.Start(t??"",10),a=Table.Buffer(#table({"CNP","Nome"},Table.Group(#"1.4 SQL Clientes","CNPJ_CPF",{"n",each {fx([CNPJ_CPF]{0}),List.Max([Nome])}},1,(x,y)=>Value.Compare(fx(x),fx(y)))[n]) in Table.AddColumn(#"1.4 SQL Clientes","Custom",each if [CNPJ_CPF] =null or [CNPJ_CPF]="" then [Nome] else a{[CNP=fx([CNPJ_CPF])]}[Nome])

Hello, brow!

I need to add a ")" before the in
The formula stayed like this

 

=let
fx=(t)=>Text.Start(t??"",10),a=Table.Buffer(#table({"CNP","Nome"},Table.Group(#"1.4 SQL Clientes","CNPJ_CPF",{"n",each {fx([CNPJ_CPF]{0}),List.Max([Nome])}},1,(x,y)=>Value.Compare(fx(x),fx(y)))[n]))
in
Table.AddColumn(#"1.4 SQL Clientes","Custom",each if [CNPJ_CPF] =null or [CNPJ_CPF]="" then [Nome] else a{[CNP=fx([CNPJ_CPF])]}[Nome])

 


 

 

I managed to create the column but returns Error

PedroAliana_0-1671805814574.png

 

Can you help me?

Thanks!

 

do i use this in the advanced editor or do i add a new column?

Anyway it is returning the error: Token Comma expected.

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.

Top Solution Authors