Forum Discussion
find the max value from a measure that returns multiple values
- 1 year ago
Just to confirm, is the format of the data something that you cannot change? You are describing calculating columns from other tables and consolidating in one table - is there a reason you are not doing this in Power Query? If not, you should reshape the data to better fit your analytical needs and then load into your model just what you need for analysis and visuals.
That said, you are on the right track with your first attempt but are missing some steps. You are getting the "multiple values supplied" error because, specifically, _union is a table in the below context but you are trying to perform string manipulation:
SUBSTITUTE(_union, UNICHAR(10), "|")Something like this should work instead.
Given these measures:
Table1 days since last checked in = ""Table2 days since last checked in = "104"Table3 days since last checked in = ""Table4 days since last checked in = "104 99 35 363"The following measure gets you the number 363:
MaxValFromText = VAR _orig = { [Table1 days since last checked in], [Table2 days since last checked in], [Table3 days since last checked in], [Table4 days since last checked in] } VAR _noEmpty = FILTER( _orig, [Value] <> "" ) VAR _toPath = CONCATENATEX( _noEmpty, SUBSTITUTE( [Value], UNICHAR(10), "|" ) , "|" ) VAR _count = PATHLENGTH( _toPath ) VAR _iter = GENERATESERIES( 1, _count ) VAR _list = GENERATE( _iter, ROW( "Item", VALUE( PATHITEM( _toPath, [Value], TEXT ) ) ) ) RETURN MAXX( _list, [Item] ) - 1 year ago
That means there are items in your line-break delimeters that cannot be converted to a number with VALUE. I'd recommend troubleshooting by switching out the final MAXX RETURN with _toPath to then inspect the pipe-delimited values you are dealing with.
If you are fine with just ignoring items that can't be converted (rather than figuring out some kind of parsing pattern to extract and then convert the number), then the following I think would work:
MaxValFromText_BlankHandling_TextHandling = VAR _orig = { [Table1 days since last checked in], [Table2 days since last checked in], [Table3 days since last checked in], [Table4 days since last checked in] } VAR _noEmpty = FILTER( _orig, [Value] <> "" ) RETURN IF( NOT ISEMPTY( _noEmpty ), VAR _toPath = CONCATENATEX( _noEmpty, SUBSTITUTE( [Value], UNICHAR(10), "|" ) , "|" ) VAR _count = PATHLENGTH( _toPath ) VAR _iter = GENERATESERIES( 1, _count ) VAR _list = GENERATE( _iter, ROW( "Item", IFERROR( VALUE( PATHITEM( _toPath, [Value], TEXT ) ), BLANK() // <------ default to blank if VALUE fails ) ) ) RETURN MAXX( _list, [Item] ) )
Just to confirm, is the format of the data something that you cannot change? You are describing calculating columns from other tables and consolidating in one table - is there a reason you are not doing this in Power Query? If not, you should reshape the data to better fit your analytical needs and then load into your model just what you need for analysis and visuals.
That said, you are on the right track with your first attempt but are missing some steps. You are getting the "multiple values supplied" error because, specifically, _union is a table in the below context but you are trying to perform string manipulation:
SUBSTITUTE(_union, UNICHAR(10), "|")
Something like this should work instead.
Given these measures:
Table1 days since last checked in =
""Table2 days since last checked in =
"104"Table3 days since last checked in =
""Table4 days since last checked in =
"104
99
35
363"
The following measure gets you the number 363:
MaxValFromText =
VAR _orig =
{
[Table1 days since last checked in],
[Table2 days since last checked in],
[Table3 days since last checked in],
[Table4 days since last checked in]
}
VAR _noEmpty = FILTER( _orig, [Value] <> "" )
VAR _toPath = CONCATENATEX( _noEmpty, SUBSTITUTE( [Value], UNICHAR(10), "|" ) , "|" )
VAR _count = PATHLENGTH( _toPath )
VAR _iter = GENERATESERIES( 1, _count )
VAR _list = GENERATE( _iter, ROW( "Item", VALUE( PATHITEM( _toPath, [Value], TEXT ) ) ) )
RETURN
MAXX( _list, [Item] )
It was working like a charm when I was filtering on a single row. When I removed the filter, I got the generate series cannot be blank error.
- MarkLaf1 year ago
Super User
That is probably happening if you have instances where all your inputs are empty strings (""). I think the below would handle it. You can see that it is mostly the same, except we ensure we aren't dealing with an empty set before moving on.
MaxValFromText_BlankHandling = VAR _orig = { [Table1 days since last checked in], [Table2 days since last checked in], [Table3 days since last checked in], [Table4 days since last checked in] } VAR _noEmpty = FILTER( _orig, [Value] <> "" ) RETURN IF( NOT ISEMPTY( _noEmpty ), VAR _toPath = CONCATENATEX( _noEmpty, SUBSTITUTE( [Value], UNICHAR(10), "|" ) , "|" ) VAR _count = PATHLENGTH( _toPath ) VAR _iter = GENERATESERIES( 1, _count ) VAR _list = GENERATE( _iter, ROW( "Item", VALUE( PATHITEM( _toPath, [Value], TEXT ) ) ) ) RETURN MAXX( _list, [Item] ) )- Neiners1 year ago
Helper II
now I am getting cannot convert value " of type text to type number.
This still works if filtering on one row
- MarkLaf1 year ago
Super User
That means there are items in your line-break delimeters that cannot be converted to a number with VALUE. I'd recommend troubleshooting by switching out the final MAXX RETURN with _toPath to then inspect the pipe-delimited values you are dealing with.
If you are fine with just ignoring items that can't be converted (rather than figuring out some kind of parsing pattern to extract and then convert the number), then the following I think would work:
MaxValFromText_BlankHandling_TextHandling = VAR _orig = { [Table1 days since last checked in], [Table2 days since last checked in], [Table3 days since last checked in], [Table4 days since last checked in] } VAR _noEmpty = FILTER( _orig, [Value] <> "" ) RETURN IF( NOT ISEMPTY( _noEmpty ), VAR _toPath = CONCATENATEX( _noEmpty, SUBSTITUTE( [Value], UNICHAR(10), "|" ) , "|" ) VAR _count = PATHLENGTH( _toPath ) VAR _iter = GENERATESERIES( 1, _count ) VAR _list = GENERATE( _iter, ROW( "Item", IFERROR( VALUE( PATHITEM( _toPath, [Value], TEXT ) ), BLANK() // <------ default to blank if VALUE fails ) ) ) RETURN MAXX( _list, [Item] ) )