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

Get certified in Microsoft Fabric—for free! For a limited time, get a free DP-600 exam voucher to use by the end of 2024. Register now

Reply
tomekm
Helper III
Helper III

Calculate if a column contains some text for each occurrence of a unique value

Hello,

 

I'm trying to come up with a formula for the following scenario. Any help would be greatly appreciated.

 

For a given Primary Key in column D ("R_PK1"), IF the Primary Key is present EXACTLY 2 times in the table, AND if column B equals "BBB" in any of the 2 rows for that Primary Key, then give me "some text" else "some other text".

 

Below is the sample data. Thank you for your help!

 

Code:Category:Type:R_PK1:Output column:
abcAAAShared1111111some text
xyzBBBShared1111111some text
cdbAAAShared2222222some other text
wsdBBBShared2222222some other text
gjkFFFShared2222222some other text

 

2 ACCEPTED SOLUTIONS
Ashish_Mathur
Super User
Super User

Hi,

This calculated column formula works

Column = if(and(CALCULATE(COUNTROWS(Data),FILTER(Data,Data[R_PK1]=EARLIER(Data[R_PK1])))=2,CALCULATE(COUNTROWS(Data),FILTER(Data,Data[R_PK1]=EARLIER(Data[R_PK1])&&Data[Category]="BBB"))>=1),"Some Text","Some other text")

Hope this helps.

Untitled.png


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/

View solution in original post

v-yangliu-msft
Community Support
Community Support

Hi  @tomekm ,

Here are the steps you can follow:

1. Create calculated column.

Output column =
var _count=COUNTX(FILTER(ALL('Table'),'Table'[R_PK1:]=EARLIER('Table'[R_PK1:])),[R_PK1:])
var _category=SELECTCOLUMNS(FILTER(ALL('Table'),[R_PK1:]=EARLIER('Table'[R_PK1:])),"1",'Table'[Category:])
return
IF(
    _count=2&&"BBB" in _category,"Some Text","Some other text")

2. Result:

vyangliumsft_0-1645769948176.png

 

Best Regards,

Liu Yang

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-yangliu-msft
Community Support
Community Support

Hi  @tomekm ,

Here are the steps you can follow:

1. Create calculated column.

Output column =
var _count=COUNTX(FILTER(ALL('Table'),'Table'[R_PK1:]=EARLIER('Table'[R_PK1:])),[R_PK1:])
var _category=SELECTCOLUMNS(FILTER(ALL('Table'),[R_PK1:]=EARLIER('Table'[R_PK1:])),"1",'Table'[Category:])
return
IF(
    _count=2&&"BBB" in _category,"Some Text","Some other text")

2. Result:

vyangliumsft_0-1645769948176.png

 

Best Regards,

Liu Yang

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

Ashish_Mathur
Super User
Super User

Hi,

This calculated column formula works

Column = if(and(CALCULATE(COUNTROWS(Data),FILTER(Data,Data[R_PK1]=EARLIER(Data[R_PK1])))=2,CALCULATE(COUNTROWS(Data),FILTER(Data,Data[R_PK1]=EARLIER(Data[R_PK1])&&Data[Category]="BBB"))>=1),"Some Text","Some other text")

Hope this helps.

Untitled.png


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/

Thank you!

You are welcome.


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/
PijushRoy
Super User
Super User

Hi @tomekm 

Please try this, (change table name and col name as per quirement)
= VAR _count2 = DISTINCTCOUNT('Table'[R_PK1])
RESULT
SWITCH(
TRUE(),
_count2 = 2 && 'Table'[Category] = "BBB", "Some Test",
"Some Other Test")


If not solved, please keep posted (paste from excel, your data is broken when I try to use)


If solve your requirement, please mark this answer as SOLUTION
If this comment helps you, please LIKE this comment/Kudos




Did I answer your question? Mark my post as a solution!
Appreciate your Like/Kudos

Proud to be a Super User!





Code:Category:Type:R_PK1:Output column:
abcAAAShared1111111some text
xyzBBBShared1111111some text
cdbAAAShared2222222some other text
wsdCCCShared2222222some other text
gjkFFFShared2222222some other text

Thank you for the quick response. Unfortunately this formula didn't work for me. I'm pasting the data in tabular format here:

 

Code:Category:Type:R_PK1:Output column:
abcAAAShared1111111some text
xyzBBBShared1111111some text
cdbAAAShared2222222some other text
wsdCCCShared2222222some other text
gjkFFFShared2222222some other text

Helpful resources

Announcements
November Carousel

Fabric Community Update - November 2024

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

Live Sessions with Fabric DB

Be one of the first to start using Fabric Databases

Starting December 3, join live sessions with database experts and the Fabric product team to learn just how easy it is to get started.

Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount! Early Bird pricing ends December 9th.

Nov PBI Update Carousel

Power BI Monthly Update - November 2024

Check out the November 2024 Power BI update to learn about new features.