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

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now! Learn more

Reply
rhiheu
Helper I
Helper I

Sales orders that contain apples Y/N

Hi,

What is the column or measure formula to generate the Y or N in the 'Does the order contain apples' column? Using a DirectQuery model.

OrderIDItemDoes the order contain apples?
001ApplesY
001BrocolliY
002PearsY
002ApplesY
002BananasY
003BananasN
003PearsN
004ApplesY

Thanks for your help.

8 REPLIES 8
wdx223_Daniel
Super User
Super User

NewColumn=IF(COUNTROWS(FILTER(Table,Table[Item]="Apples"&&Table[OrderID]=EARLIER(Table[OrderID])))>0,"Y","N")

Hi, thanks for your reply, unfortunately I get an error when I try that - 'EARLIER/EARLIEST refers to an earlier row context which doesn't exist'.

my code is to create a calculated column, not a measure

if to create a measure, try this

=IF(CALCULATE(COUNTROWS(Table),Table[Item]="Apples",ALLEXCEPT(Table,Table[OrderID]))>0,"Y","N")

Hi, when I tried the original formula as a column, it said 'Function 'COUNTROWS' is not allowed as part of calculated column DAX expressions on DirectQuery models', and the latter formula as a measure it doesn't error when creating it but says 'can't display the visual' when I add it to a table. Sorry I should of mentioned in my original post I'm using Direct Query, I'll add that in now. So I'm not sure if there is a solve for this using DAX, maybe only SQL?

tamerj1
Super User
Super User

Hi @rhiheu 
Please try

contain apples =
IF (
    "Apple"
        IN CALCULATETABLE (
            VALUES ( 'Table'[Item] ),
            ALLEXCEPT ( 'Table', 'Table'[OrderID] )
        ),
    "Y",
    "N"
)

Hi @tamerj1, thanks for your reply. I've inputted that formula and it doesn't error, but then when I try and add it to my table it says 'Can't display the visual' - I'm using a Direct Query model if that makes a difference?

@rhiheu 
If you are creating a measure, then please clarify the existing filter context.

Hi, If I use your formula for a column it says 'Function 'CONTAINSROW' is not allowed as part of calculated column DAX expressions on DirectQuery models' which is odd since 'CONTAINSROW' is not actually in the formula.

Re: filter context, I'm not sure if I understand the question sorry - I have a date filter applied, but I want this formula to ignore all filters.

Helpful resources

Announcements
Power BI DataViz World Championships

Power BI Dataviz World Championships

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now!

December 2025 Power BI Update Carousel

Power BI Monthly Update - December 2025

Check out the December 2025 Power BI Holiday Recap!

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.