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
Anonymous
Not applicable

How to use successive table VARiables?

I am trying to build up a complex DAX measure by doing separate steps into table variables. The measure uses VARs tmp1 thru tmp5, all building on the previous tmp VAR tables. I am now trying to create VAR tmp6:

 

 

VAR tmp6 = SUMMARIZE(tmp5, [AccountNo], [StartDate], "FirstAnotherDate", MIN([AnotherDate]))

 


but Power BI gives me this error: Column 'AnotherDate' cannot be found or used in this expression.

I don't understand tbecause both [StartDate] and [AnotherDate] fields were referenced in the VAR tmp5 definition with no error.
Any ideas?

1 ACCEPTED SOLUTION

@Anonymous - So what happens if you just return tmp5 in a Table formula, what does it look like? Also, you might try GROUPBY instead of SUMMARIZE

 

VAR tmp6 = GROUPBY(tmp5, [AccountNo], [StartDate], "FirstAnotherDate", MINX(CURRENTGROUP(),[AnotherDate]))

 



Follow on LinkedIn
@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
DAX For Humans

DAX is easy, CALCULATE makes DAX hard...

View solution in original post

8 REPLIES 8
Greg_Deckler
Community Champion
Community Champion

@Anonymous - Would need to see the whole formula. But perhaps you should be using ADDCOLUMNS here.



Follow on LinkedIn
@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
DAX For Humans

DAX is easy, CALCULATE makes DAX hard...
Anonymous
Not applicable

Here is the whole DAX formula. Each step up to tmp5 works. Only when I add step tmp6 do I get the error.

Edit: I've added comments to each step to explain what I'm trying to do.

 

 

 

 

-- Get all the rows from fact table 1 where the flag matches. Only get data for test account 12345.
VAR tmp1 = FILTER(ALL('FactTable1'), RELATED('DimensionTable1'[Dim1Attr1]) = "Data of interest" && RELATED('Account'[FullAccountNumber]) = 12345)

-- For each account, get the earliest of StartDate.
-- We need to create a new column for "AccountNo" with no lineage by adding 0. This allows us to join on this column at step tmp4.
VAR tmp2 = SELECTCOLUMNS(
    SUMMARIZE(tmp1, [AccountNo], "StartDate", MIN([StartDate])),
    "AccountNo", [AccountNo] + 0,
    "StartDate", [StartDate]
)

-- Get all the rows from fact table 2 where the flag matches.
-- We need to create a new column for "AccountNo" with no lineage by adding 0. This allows us to join on this column at step tmp4.
VAR tmp3 = SELECTCOLUMNS(
    FILTER(ALL('FactTable2'), RELATED('DimensionTable3'[Dim3Attr1]) = "Other data of interest"),
    "AccountNo", [AccountNo] + 0,
    "AnotherDate", [AnotherDate]
)

-- Join the fact tables together across [AccountNo].
VAR tmp4 = NATURALLEFTOUTERJOIN(tmp2, tmp3)

-- Only interested in data where [AnotherDate] >= [StartDate]
VAR tmp5 = FILTER(tmp4, [AnotherDate] >= [StartDate])

-- For each [AccountNo] and [StartDate], get the earliest date of [AnotherDate]
VAR tmp6 = SUMMARIZE(tmp5, [AccountNo], [StartDate], "FirstAnotherDate", MIN([AnotherDate]))

 

 

 

 

@Anonymous - So what happens if you just return tmp5 in a Table formula, what does it look like? Also, you might try GROUPBY instead of SUMMARIZE

 

VAR tmp6 = GROUPBY(tmp5, [AccountNo], [StartDate], "FirstAnotherDate", MINX(CURRENTGROUP(),[AnotherDate]))

 



Follow on LinkedIn
@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
DAX For Humans

DAX is easy, CALCULATE makes DAX hard...
Anonymous
Not applicable

Using the GROUPBY with CURRENTGROUP() worked. Thank you, @Greg_Deckler 

Anonymous
Not applicable

I have another problem with this measure. VAR tmp3 must only look at data for the [AccountNo] that exist in tmp1, as it is very slow if it loads for every AccountNo when only a few have been selected in the filter. I have tried this in the filter for tmp3 as per below, but it gives an error: "The syntax for 'in' is incorrect"

How can this be achieved?

-- Get all the rows from fact table 1 where the flag matches. Only get data for test account 12345.
VAR tmp1 = FILTER(ALL('FactTable1'), RELATED('DimensionTable1'[Dim1Attr1]) = "Data of interest" && RELATED('Account'[FullAccountNumber]) = 12345)

-- For each account, get the earliest of StartDate.
-- We need to create a new column for "AccountNo" with no lineage by adding 0. This allows us to join on this column at step tmp4.
VAR tmp2 = SELECTCOLUMNS(
    SUMMARIZE(tmp1, [AccountNo], "StartDate", MIN([StartDate])),
    "AccountNo", [AccountNo] + 0,
    "StartDate", [StartDate]
)

-- Get all the rows from fact table 2 where the flag matches.
-- We need to create a new column for "AccountNo" with no lineage by adding 0. This allows us to join on this column at step tmp4.
VAR tmp3 = SELECTCOLUMNS(
    FILTER(ALL('FactTable2'),
	RELATED('DimensionTable3'[Dim3Attr1]) = "Other data of interest"
	&& 'FactTable2'[AccountNo] IN VALUES(tmp1[AccountNo])
	),
    "AccountNo", [AccountNo] + 0,
    "AnotherDate", [AnotherDate]
)

-- Join the fact tables together across [AccountNo].
VAR tmp4 = NATURALLEFTOUTERJOIN(tmp2, tmp3)

-- Only interested in data where [AnotherDate] >= [StartDate]
VAR tmp5 = FILTER(tmp4, [AnotherDate] >= [StartDate])

-- For each [AccountNo] and [StartDate], get the earliest date of [AnotherDate]
VAR tmp6 = SUMMARIZE(tmp5, [AccountNo], [StartDate], "FirstAnotherDate", MINX(CURRENTGROUP(),[AnotherDate])

 

@Anonymous - That's kind of odd honestly, I don't suppose you could share some sample data, I'd love to tinker with this.



Follow on LinkedIn
@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
DAX For Humans

DAX is easy, CALCULATE makes DAX hard...
amitchandak
Super User
Super User

@Anonymous , Another date is used in min expression renamed as FirstAnotherDate, try to use FirstAnotherDate.

Share with Power BI Enthusiasts: Full Power BI Video (20 Hours) YouTube
Microsoft Fabric Series 60+ Videos YouTube
Microsoft Fabric Hindi End to End YouTube
Anonymous
Not applicable

The field [FirstAnotherDate] does not exist yet. I am trying to create it in VAR tmp6. The SQL equivalent of what I am trying to achieve is this:

 

SELECT [AccountNo], [StartDate], MIN([AnotherDate]) AS [FirstAnotherDate])
FROM tmp5
GROUP BY [AccountNo], [StartDate]

 

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!

November Power BI Update Carousel

Power BI Monthly Update - November 2025

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

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.