I have the following code, which sums up all my fees for my clients.
Code Snippert:
SumAmtVar =
SUMX (
FILTER ( _billings, _billings[_tsg_clientid_value] = accounts[accountid] ),
__billings[Amount]
)
What I need is an additional filter that filters my transaction dates for the current month or a hard coded date range. What would the second filter look like? Any help or assistance is greatly appreciated.
You can put conditions together using the AND() function or more simply using &&.
For example,
SUMX(
FILTER( Table1, <Condition 1> && <Condition 2> && <Condition 3> ),
<Expression>
)
Related
Please help to calculate/understand properly lastDate and rankDate measures for following simplified example (download):
Desired result:
Reality (incorrect subtypes):
Why relationship is broken?
How to avoid this cartesian product lines?
My Measure (I commented workaround, because it's kind of postfilter, not prefilter):
rnkDate =
VAR t =
CALCULATETABLE(
VALUES(tstTable[Date]),
REMOVEFILTERS(tstTable[Date])
)
RETURN
//IF( MAX(tstTable[Amount])<>BLANK(), // WORKAROUND To hide unwantedd rows
RANKX(
t,
LASTDATE(tstTable[Date])
)
//)
P.S. Mess happens only if I use fields from dimensional table dimType[Type] (within one table everything is Ok):
The problem is that the query generated by Power BI performs the cartesian product and filers the result by checking the result of the measure.
in our case is something similar to
SUMMARIZECOLUMNS(
'dimType'[Type],
'tstTable'[subType],
'tstTable'[Date],
"MinAmount", CALCULATE(MIN('tstTable'[Amount])),
"lastDate", 'tstTable'[lastDate],
"rnkDate", 'tstTable'[rnkDate]
)
SUMMARIZECOLUMNS doesn't use relationships when iterating on different tables, it applies them when evaluating the measures. There is an article explaining what is the equivalent DAX code executed by SUMMARIZECOLUMNS
Introducing SUMMARIZECOLUMNS
the problem is that RANKX evaluated on an empty table retuns 1. This can be seen executing this on dax.do
EVALUATE
VAR t =
FILTER ( ALL ( 'Date'[Date] ), FALSE )
RETURN
{ RANKX ( t, [Sales Amount] ), CALCULATE ( [Sales Amount], t ) }
so the solution is to first check that the table t is not empty, which is the reason because the workaround that you implemented solved the issue
lastDate =
IF( NOT ISEMPTY(tstTable), // checks fact table in particular context
CALCULATE(
LASTDATE(tstTable[Date]),
REMOVEFILTERS(tstTable[Date])
)
)
rnkDate =
VAR t =
CALCULATETABLE(
VALUES(tstTable[Date]),
REMOVEFILTERS(tstTable[Date])
)
RETURN
IF( NOT ISEMPTY(tstTable),
RANKX(
t,
LASTDATE(tstTable[Date])
)
)
I am fairly new to Power BI and DAX and I am struggling with getting the below measure to work (I have provided to different version of what I have tried). I am trying to COUNTA the # of items that met certain criteria in two separate columns. Each column that the measure looks at has at least 3 filter criteria and in column two I need to use wildcards as the data there in has entries that share the first two values but the 3rd value could change over time. Any assistance would be amazing as I have tried dozens of different ways and I still cannot figure it out.
GA = CALCULATE(COUNTA(Table,[Item]),filter(Table,Table[Column 1]in{"GA","SP"}) , Filter(Table,Table[Column2]in{"MT*","GA*","SP*","OP*"}))
GA = CALCULATE(COUNTA(Table[Item]),Table[Column1]= "GA" || Table[Column1]= "SP" && Table[Column2]="GA*" || Table[Column2]= "OP*" || Table[Column2]]= "SP*" || Table[Column2]= "MT*")
Unfortunately I cannot provide an example of the data.
Thank you in advance
As #Alexis Olson mentions in his reply to this post, there does not appear to be a shorthand way to use the IN operator with wildcards.
Hence:
GA =
CALCULATE(
COUNTROWS( 'Table' ),
FILTER(
'Table',
'Table'[Column1]
IN { "GA", "SP" }
&& (
CONTAINSSTRING( 'Table'[Column2], "MT" )
|| CONTAINSSTRING( 'Table'[Column2], "GA" )
|| CONTAINSSTRING( 'Table'[Column2], "SP" )
|| CONTAINSSTRING( 'Table'[Column2], "OP" )
)
)
)
I need to convert a DAX expression I'm using i PowerBI to MDX to use in Excel > Olap > Calculated Measure (or member?). I'm no developer and do not have full access to the database, but connect to it using analysis services.
This DAX measure is working. I want to the same logic in Excel as a calculated measure (or calculated member?). Running measure displays sales quantity where the products has a 1 in either FilterA, FilterB or FilterC.
CALCULATE(
[SalesQtn],
FILTER(
'Product',
'Product'[FilterA] = "1" || 'Product'[FilterB] = "1" || 'Product'[FilterC] = "1"
)
)
I'm 98% newbie in In DAX and 100% newbie in MDX. I've managed to do like below in Calculated Measure to filter using FilterA = "1", but I recon a function is needed for combination FilterB and C using OR operation
([Measures.[SalesQtn],
[Product].[FilterA].&[1])
Try the following:
SUM(
{
([Product].[FilterA].&[1], [Product].[FilterB].[All], [Product].[FilterC].[All]),
([Product].[FilterA].[All], [Product].[FilterB].&[1], [Product].[FilterC].[All]),
([Product].[FilterA].[All], [Product].[FilterB].[All], [Product].[FilterC].&[1])
},
[Measures].[SalesQtn]
)
Or if there are some rows where two FilterA and FilterB are 1 that might produce a wrong result. In that case try:
Sum(
Filter(
[Product].[FilterA].[FilterA].Members
* [Product].[FilterB].[FilterB].Members
* [Product].[FilterC].[FilterC].Members,
[Product].[FilterA].CurrentMember is [Product].[FilterA].&[1]
Or [Product].[FilterB].CurrentMember is [Product].[FilterB].&[2]
Or [Product].[FilterC].CurrentMember is [Product].[FilterC].&[1]
),
[Measures].[SalesQtn]
)
I've got about 20 different metrics across 10 locations and want to make a matrix with metrics as rows and the locations as the different columns. The issue I'm running into is that the metrics are different data types, some are whole numbers, some are %s and some are $s.
Is there any way to custom format each row as a different data type like there is in excel?
Edit: Sorry I wasn't clear. I don't want the same value showing up multiple times.
See below screenshots.
Test Data Screenshot:
What I want, but I want it in Power BI, not Excel:
What I don't want when I use measures that are formatted as different data types:
The formatting is not controlled by the rows or columns but rather each measure can be assigned its own data type using the Modeling tab.
Edit: I see a few options here.
Option 1: Write a text measure that switches formats like this:
FormatMetric =
VAR Val = SUM ( TestData[Value] )
RETURN
SWITCH (
SELECTEDVALUE ( TestData[Metric] ),
"# quantity", FORMAT ( Val, "0" ),
"$ Sales", FORMAT ( Val, "$0.00" ),
"% to plan", FORMAT ( Val, "0%" )
)
You'll get a table that looks like this:
Be aware that this measure returns text values and won't work in a chart.
Option 2: Create three separate measures and format each separately:
# quantity = CALCULATE ( SUM ( TestData[Value] ), TestData[Metric] = "# quantity" )
$ Sales = CALCULATE ( SUM ( TestData[Value] ), TestData[Metric] = "$ Sales" )
% to plan = CALCULATE ( SUM ( TestData[Value] ), TestData[Metric] = "% to plan" )
If you make sure you have Format > Values > Show on rows turned on and put these three measures in the Values box:
These measures can be used in charts.
Option 3: Pivot your data table on the Metric column in the query editor so you don't have mixed data types in a single column. Your data table should look like this now:
From here, you can write three simple measures format as in the previous option:
# quantity = SUM ( Pivot[# quantity] )
$ Sales = SUM ( Pivot[$ Sales] )
% to plan = SUM ( Pivot[% to plan] )
I thought I'd reach out for help as I'm having troubles creating a calculated measure in PowerBI. I'm trying to calculate the sum of multiple accounts using filters to grab the correct accounts.
Here is the measure that isn't working:
Measure =
CALCULATE (
SUM ( 'Queryx'[Amount] ),
FILTER (
Queryx,
LEFT ( 'Queryx'[Accnt], 4 )
IN { "8980", "8981", "8982", "8983", "8987", "8988", "8989" }
),
FILTER ( 'Queryx', [Accnt] IN { "89660", "89700", "89850" } ),
FILTER ( 'Queryx', LEFT ( 'Queryx'[Accnt], 3 ) IN { "899" } )
)
If I run the measure with only 1 filter the measure works, but the addition of the other 2 filters throws a wrench in the system. Any idea how I could get this to run? Or is there a smarter way to go about creating this measure?
Thanks for taking the time to read this and I greatly appreciate any help =)
The filters are all applied using AND logic. I suspect you want to use OR logic since it's not possible to satisfy all those conditions at once.
Try something more like this:
Measure =
CALCULATE (
SUM ( 'Queryx'[Amount] ),
FILTER (
Queryx,
LEFT ( 'Queryx'[Accnt], 4 )
IN { "8980", "8981", "8982", "8983", "8987", "8988", "8989" }
|| [Accnt] IN { "89660", "89700", "89850" } ),
|| LEFT ( 'Queryx'[Accnt], 3 ) IN { "899" } )
)
)
The || is the logical OR syntax.