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

Enhance your career with this limited time 50% discount on Fabric and Power BI exams. Ends August 31st. Request your voucher.

Reply
xtwert55
New Member

Finding and max value over two variables in table.

Hello, I have been stuck on this problem for a few days now.

I have a table of elements each with a bunch RAG status', date of RAG assessment and country of origin. 

 

sheet1 (snippet of table)

ElementRAGDateCountry
ARED      01/02/24    USA
AAMBER        01/03/24    USA
AGREEN   01/04/24   Mexico
ARED   01/05/24   Mexico 
BGREEN  01/06/24   USA
BRED  01/07/24   Mexico
BRED  01/08/24   USA


I want a DAX measure that counts the most recent RAG status for each element over each country. For instance for element A the most recent RAG for USA is AMBER (01/03/24) and the most recent for Mexico is RED (01/05/24). The reuslting table should look like below.


The resulting table should show:

Element RAG(DAX) Count of most recent RAG
A    AMBER  1
ARED  1
BRED  

2


The reason I am contrained on what columns to use is because I want to put these as fields in a stacked bar chart.
x= element,
y = count ,
legend = status

I cannot create a summarized table as I need the table to be dynamic with a date slicer. I have written some DAX to achieve this but the reuslts are not as I expected.

I first created a virtual SUMMARIZE table to find the most recent RAG for each element and country. Then filtering the virtual table on sheet1 using TREATAS while also filtering on the current row conext using SELECTEDVALUE and then counting the result. 

 

 

 

 var _element = SELECTEDVALUE(Sheet1[Element])
 var _rag = SELECTEDVALUE(Sheet1[RAG])
 var _vtable = 
    SUMMARIZE(
        sheet1, 
        sheet1[Element], 
        sheet1[Country], 
        "RAG", CALCULATE(		
        	FIRSTNONBLANK(sheet1[RAG], 1), 
        		FILTER(
            		sheet1, 
            		sheet1[Date] = MAX(sheet1[Date]) 
			 
       		 )
    	)
    )
var _latest = FILTER(TREATAS ( _vtable, 
Sheet1[Element], 
Sheet1[Country],
Sheet1[RAG]),  
Sheet1[Element] = _element && 
Sheet1[RAG] = _rag)

return
CALCULATE(COUNT(Sheet1[RAG]),_latest)

 

 

I added this dax to a table with columns sheet1[element], sheet1[rag], DAX_Measure. It appears to completly ingore the row context and produces nonsense. 

Do you know what I am doing wrong? Any help would be appricated. 

2 ACCEPTED SOLUTIONS
johnt75
Super User
Super User

You can use

Count of most recent RAG =
VAR CurrentStatus = SELECTEDVALUE('Table'[RAG])
VAR BaseTable = CALCULATETABLE(
	'Table',
	ALLEXCEPT(
		'Table',
		'Table'[Element]
	)
)
VAR SummaryTable =
INDEX(
	1,
	BaseTable,
	ORDERBY(
		'Table'[Date],
		DESC
	),
	PARTITIONBY('Table'[Country]),
	MATCHBY(
		'Table'[Country],
		'Table'[Date]
	)
)
VAR Result = COUNTROWS(FILTER(
	SummaryTable,
	'Table'[RAG] = CurrentStatus
))
RETURN
	Result

View solution in original post

Do you already have a table called SummaryTable in your model? If so, just use _SummaryTable in the measure instead.

View solution in original post

5 REPLIES 5
FreemanZ
Super User
Super User

hi  @xtwert55 ,

 

try like:

Measure =

VAR _table =

ADDCOLUMNS(

    SUMMARIZE(

        data, data[Element], data[Country]

    ),

    "LastRag",

    VAR _date = CALCULATE(MAX(data[date]))

    RETURN CALCULATE(MAXX(FILTER(data, data[date]=_date), data[RAG]))

)

VAR _result =

COUNTROWS(

   SUMMARIZE(_table, [LastRag])

)

RETURN _result

johnt75
Super User
Super User

You can use

Count of most recent RAG =
VAR CurrentStatus = SELECTEDVALUE('Table'[RAG])
VAR BaseTable = CALCULATETABLE(
	'Table',
	ALLEXCEPT(
		'Table',
		'Table'[Element]
	)
)
VAR SummaryTable =
INDEX(
	1,
	BaseTable,
	ORDERBY(
		'Table'[Date],
		DESC
	),
	PARTITIONBY('Table'[Country]),
	MATCHBY(
		'Table'[Country],
		'Table'[Date]
	)
)
VAR Result = COUNTROWS(FILTER(
	SummaryTable,
	'Table'[RAG] = CurrentStatus
))
RETURN
	Result

Thank you for response however I get the error: 'SummaryTable' is a table name and cannot be used to define a variable. When i write line 26: 'sheet1'[RAG] = CurrentStatus

error.PNG

Do you already have a table called SummaryTable in your model? If so, just use _SummaryTable in the measure instead.

You are a hero! Yes this seems to be working perfectly now. Thank you very much 🙂

Helpful resources

Announcements
July 2025 community update carousel

Fabric Community Update - July 2025

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

July PBI25 Carousel

Power BI Monthly Update - July 2025

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

Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.