This question has been asked and answered before, however the solutions I've found don't seem to work for my current situation.
My table looks like:
I want to return the highest value in column B for all instances of it's match in column A in power pivot.
With a standard excel function, I would use =max(if(a2=a:a,b:b)) in column C.
I've tried =CALCULATE(max(Table1[B]),filter(Table1,Table1[A]=Table1[A])) but this is the result
Any help would be appreciated!
Table1[A] always equals Table1[A] so your condition is always true and thus doesn't do any meaningful filtering.
What you're looking for is to filter by the earlier row context (from the original table, not the FILTER iterator function):
CALCULATE (
MAX ( Table1[B] ),
FILTER ( Table1, Table1[A] = EARLIER ( Table1[A] ) )
)
Another way to do this is to use a variable to grab the row context before you're inside of the FILTER.
VAR CurrRowA = Table1[A]
RETURN
CALCULATE ( MAX ( Table1[B] ), Table1[A] = CurrRowA )
Related
I have a table that looks like this:
I've loaded this into Excel PowerPivot and am trying to create a normalized/indexed column that I can use in pivot table. For reasons I wont go into, I'm trying to do this via a Measure vs a Calculated Column.
Here is the code for my Measure:
Measure 1:=VAR Group1 = GROUPBY('Test','Test'[Country],'Test'[Date],"Visits1", SUMX(CURRENTGROUP(),'Test'[Visits])) VAR Group2 = GROUPBY(Group1,[Country],"Visits2", MAXX(CURRENTGROUP(),[Visits1])) RETURN CALCULATE(MAXX(Group2,[Visits2]),ALL('Test'[Date]))
I'm pretty novice with DAX, but this is what I assume is happening when I create my Table VARs:
Using this Measure, when I go and create a Pivot Table, I get this (I've manually added the yellow columns):
As you can see, 'Measure 1', does not equal my 'Expected' column. I've tried a whole bunch of ways to use ALL() in my RETURN statement, but I cannot get it to work. Can anyone help?
Oh, and once I achieve this my goal was to indexed value as seen in my final yellow column using the pseudo formula below.
Row value / Max column value (but filtered by country) * 100
[measure] :=
VAR MaxVisits =
CALCULATE (
MAXX ( VALUES ( Visits[Date] ), CALCULATE ( SUM ( Visits[Visits] ) ) ),
ALL ( Visits[Date] )
)
VAR CurrentVisit =
CALCULATE (
MAXX ( VALUES ( Visits[Date] ), CALCULATE ( SUM ( Visits[Visits] ) ) )
)
VAR Ratio =
DIVIDE ( CurrentVisit, MaxVisits ) * 100
RETURN
Ratio
I am searching for a DAX formula.
Specifically:
If SOLD (1st column) = count volume (second column), if not SOLD = 0
I need to reflect the volume of SOLD in a new column. UNSOLD Volumes should be 0.
I attach the reduced data set.
If I understand, you want to only perform aggregation when the SOLD_UNSOLD column is equal to "SOLD", otherwise return 0? If so, the following formula will do this, you'll just need to update the column names accordingly. The outer IF prevents problems resulting from further evaluation (i.e. grand totals) and it's necessary to wrap the column in the VALUES function as this will turn the column into a table of the unique values of it.
'YourTable'[CountOfSold] =
IF (
COUNTROWS ( VALUES ( YourTable[SOLD_UNSOLD] ) ) = 1,
IF (
VALUES ( YourTable[SOLD_UNSOLD] ) = "SOLD",
COUNT ( YourTable[ColumnToAggregate] ),
0
),
0
)
The Excel SUMIF is closest to the DAX SUMX.
I'm not positive I'm understanding what you are asking for, but I think you'd want something like this:
SOLD_VOLUME = SUMX(Table1,
IF(Table1[SOLD_UNSOLD] = "SOLD",
Table1[DAILY_VOLUME],
0
)
)
You could also do this with a filter:
SOLD_VOLUME = SUMX(
FILTER(
Table1,
Table1[SOLD_UNSOLD] = "SOLD"
),
Table1[DAILY_VOLUME]
)
Similarly to the Basket Analysis DAX pattern model, I have 1 fact for Sales, 1 dimension for Product and an extra dimension for Filter Product.
I want to use the Filter Product dimension to exclude products chosen by the user. I made it work with this DAX formula:
Sales =
CALCULATE (
SUM ( Sales['Sales'] ),
FILTER (
Product,
NOT ( 'Product'['ProductName'] IN VALUES ( 'FilterProduct'['ProductName'] ) )
)
)
This works as long as the user has already chosen a Product to exclude on FilterProduct slicer. But if nothing has been selected, the calculation will show blank, rather than just show everything. I wonder if there's a way to handle this gracefully. An idea I had was to create a variable and see if FilterProduct ISFILTERED(). If so, copy&paste the above with the FILTER() on SWITCH statement, if not, just skip the FILTER(). But this isn't great, because it duplicates code, and if I was to add another optional filter (e.g. SalesRegion), I'd had to pre-calculate all the combinations (e.g. SalesRegion & Product, just SalesRegion, just Product, none).
I think you can use the ISFILTERED function, but not exactly how you were suggesting. Try inserting it into you measure like this:
Sales =
CALCULATE (
SUM ( Sales['Sales'] ),
FILTER (
Product,
NOT ( ISFILTERED('FilterProduct'['ProductName']) ) ||
NOT ( 'Product'['ProductName'] IN VALUES ( 'FilterProduct'['ProductName'] ) )
)
)
I have the following data source:
My pivot rows are Team => Project Name with "Value" column in the Values. I am calculating the % ration of all projects that have value "True" compared to all projects that have a value (disregarding those without values). Here's the formula I use in PowerPivot:
=CALCULATE(COUNTROWS(),'Table'[Value]=TRUE()) / CALCULATE(COUNTROWS(), ('Table'[Value]=FALSE() || 'Table'[Value]=TRUE()), ISLOGICAL('Table'[Value]))
The formula works, however I only need to see this percentage on the "Team" level, the expanded projects should still have "True/False" values. Is this possible? Preferably, without VBA.
Format your code. If you like reading very long lines, that's fine, but use DAX Formatter for the rest of us.
True vs All =
CALCULATE(
COUNTROWS( 'Table' ) // It's considered a best practice
// to explicitly name the table in
// COUNTROWS()
,'Table'[Value]=TRUE()
) / CALCULATE(
COUNTROWS( 'Table' )
// You can remove the test for [Value] = TRUE() ||
// [Value] = FALSE()
,ISLOGICAL('Table'[Value])
)
ConditionalDisplay =
IF(
ISFILTERED( 'Table'[Project] )
&& HASONEVALUE( 'Table'[Project] )
,VALUES( 'Table'[Value] )
,[True vs All]
)
[True vs All] is a cleaned up version of your existing measure.
[ConditionalDisplay] does what its name says. Displays a different value based on conditions.
We check for ISFILTERED() to cover the edge case where a given value of [Team] has only a single project. We check for HASONEVALUE() to cover the case where an explicit filter (slicer or filter) exists on [Project], but more than one is in context (grand total level).
When the two are true, we return VALUES( 'Table'[Value] ), the column made up of the distinct values in [Value]. This is only evaluated when we already know there's exactly one distinct value. A 1x1 table is implicitly converted to scalar in DAX.
When there's more than one distinct value of [Value] or it's not filtered, then we return your original measure.
[ConditionalDisplay] will fail if you have two rows for the same value of [Project] with multiple values of [Value].
Previously, I asked about how we can fetch a simple previous row through an incremented ID field (Thank you Petr HavlĂk). In this case I have ID and ACTIVITY, where (ACTIVITY&ID) is the unique value per row.
From an SQL perspective I just do an inner join where ACTIVITY = Joined ACTIVITY and ID = ID - 1 in the joined table and get the row I need.
In other words, I want the previous percentage belonging to the same activity.
So using the answer in the previous post I was able to get the result I want on 1000 rows. However if I were to increase this number of rows to 85000+ this function is dauntingly slow.
=SUMX(FILTER ( Query, (EARLIER ( [ID] ) = [ID] + 1)&&(EARLIER([ACTIVITY])=[ACTIVITY])),[PERCENTAGE])
My end result is to make this function on up to 7 million rows, if this is possible, how I can optimize it ? And if it isn't, could you explain to me why I can't do it ?
One option could be to try a variation on the approach - without your dataset I can't test whether it is more efficient but I've run similar things on 1m+ row datasets without issue:
=
CALCULATE (
SUM ( [PERCENTAGE] ),
FILTER (
Query,
[ID] = EARLIER ( [ID] ) - 1
&& [ACTIVITY] = EARLIER ( [ACTIVITY] )
)
)
Probably not what you want to hear but doing this with SQL on import is probably your best bet.
The best answer here would be using Lookupvalue, that would bypass any filters you need to do and allows you to do a direct lookup of values in the table. This would be much faster.
It would look something like:
=LOOKUPVALUE(table[PERCENTAGE], [ID] = EARLIER ( [ID] ) - 1)
Please make sure the ID values are unique as lookupvalue can only return a single result, when more than one rows are returned it will error out. You can potentially wrap it around with iserror
= IF(ISERROR(LOOKUPVALUE(table[PERCENTAGE], [ID] = EARLIER ( [ID] ) - 1)), BLANK()
, LOOKUPVALUE(table[PERCENTAGE], [ID] = EARLIER ( [ID] ) - 1)
)
)
JShmay,
this is pretty much the same question - and as Jacob has suggested, you can use logical operators that are normally available in Excel/PowerPivot.
You can really go crazy with this and should you need something more complex - for example to get difference between two points following some other condition, I would point you to very similar questions and my answers to them:
Can I compare values in the same column in adjacent rows in PowerPivot?
2nd latest Date - DAX
Hope this helps :)