Performance enhancement in DAX query - dax

I have Power BI DAX query used in a measure. It takes longer time to execute. Can anyone please help me with this?
MEASURE FACT_CONSOL_BALANCE_OL[Measure 4] =
SWITCH (
TRUE (),
CONTAINS (
DIM_ANALYTIC_STRUCT_ACCOUNT,
DIM_ANALYTIC_STRUCT_ACCOUNT[STRUCTURE_NODE (groups)], "1 - CURRENT ASSETS"
), SUM ( FACT_CONSOL_BALANCE_OL[BALANCE] ),
CONTAINS (
DIM_ANALYTIC_STRUCT_ACCOUNT,
DIM_ANALYTIC_STRUCT_ACCOUNT[STRUCTURE_NODE (groups)], "2 - NON - CURRENT ASSETS"
), SUM ( FACT_CONSOL_BALANCE_OL[BALANCE] ),
SUM ( FACT_CONSOL_BALANCE_OL[BALANCE] ) * -1
)
Performance Result on DAX Studio:

Can you please try this code, see if It solves your problem. I tried to write it without contains() function.
MEASURE FACT_CONSOL_BALANCE_OL[Measure 4] =
SUMX (
FACT_CONSOL_BALANCE_OL,
VAR Balance =
SWITCH (
RELATED ( DIM_ANALYTIC_STRUCT_ACCOUNT[STRUCTURE_NODE (groups)] ),
"1 - CURRENT ASSETS", FACT_CONSOL_BALANCE_OL[BALANCE],
"2 - NON - CURRENT ASSETS", FACT_CONSOL_BALANCE_OL[BALANCE],
FACT_CONSOL_BALANCE_OL[BALANCE] * -1
)
RETURN
Balance
)

Related

Copying a jmp script from one table to another breaks the script

I am running multiple data analysis scripts with are often oneway analyses or t-tests.
I have written or generated many of these myself but I have run into a recurring problem.
I will often generate the code while on one data table and then when I paste it to another data table with the same headings the script replaces single or multiple variables with "As Column()"
So if I have this:
Oneway(
Y( :"parameter 1" ),
X( :"parameter 2" ),
Each Pair( 1 ),
Means( 1 ),
Mean Diamonds( 1 ),
SendToReport(
Dispatch( {}, "Oneway Anova", OutlineBox, {Close( 1 )} ),
Dispatch(
{"Means Comparisons", "Comparisons for each pair using Student's t"},
"LSD Threshold Matrix",
OutlineBox,
{Close( 1 )}
),
Dispatch(
{"Means Comparisons", "Comparisons for each pair using Student's t"},
"Connecting Letters Report",
OutlineBox,
{Close( 1 )}
)
)
)
I end up with this non-functioning code after pasting it
Oneway(
Y( :"parameter 1" ),
X( As Column() ),
Each Pair( 1 ),
Means( 1 ),
Mean Diamonds( 1 ),
SendToReport(
Dispatch( {}, "Oneway Anova", OutlineBox, {Close( 1 )} ),
Dispatch(
{"Means Comparisons", "Comparisons for each pair using Student's t"},
"LSD Threshold Matrix",
OutlineBox,
{Close( 1 )}
),
Dispatch(
{"Means Comparisons", "Comparisons for each pair using Student's t"},
"Connecting Letters Report",
OutlineBox,
{Close( 1 )}
)
)
)
Another issue that happens is the original working code will sometimes not open correctly when run directly but it will work if I click edit and can see the code.
Is there something I am doing incorrectly? I am running JMP 15.0.0
TLDR: I tried to paste code from one data table to another with the same headings. I expected it to work but it broke in an unexpected way.

Calculate running total for a prior year

The following code works properly, calculates the sum of CLIN Total for all of FY 2021. I am trying to calculate the sum up to today's date, i.e., if today is July 01,2022, I want to calculate the sum for FY 2021 through July 01, 2021.
Cum FY21 Invoice Total =
CALCULATE(
SUM('Invoice Amounts'[CLIN Total]),
FILTER(
ALL('Calendar'),
'Calendar'[Fiscal Year] = 2021
&& 'Calendar'[Date] < MAX('Calendar'[Date])
&& 'Calendar'[Date] < NOW())
)
I tried many variations of the following to replace the second filter, but can't find the right command. Dax doesn't seem to accept the now() in this context, but I'm not sure what the right one should be.
&&'Calendar'[Date]< DATEADD(now(),-1,YEAR)
This approach should help you to solve the issue.
VAR FirstDayThisYear =
SAMEPERIODLASTYEAR(STARTOFYEAR(Calendar'[Date])
VAR LastDayThisYear =
SAMEPERIODLASTYEAR(
LASTDATE(Calendar'[Date])
)
VAR SetOfDates=
DATESBETWEEN(
Calendar'[Date]
,FirstDayThisYear
,LastDayThisYear
)
RETURN
CALCULATE(
SUM('Invoice Amounts'[CLIN Total]),
,SetOfDates=
)

Power BI DAX : multi-column and multi-row condition and group by 2 columns

Folks, I am trying to create a calculated column/measures and experiencing issues.
My Data-set looks like this:
City
Building Name
Test Date
Component
Test Result
Calculated Result
-
-
-
-
-
-
City1
Build1
1/3/2014
Component A
Pass
None
City1
Build1
1/11/2014
Component 1
Fail
Fail1
City1
Build1
1/11/2014
Component 2
Pass
Fail1
City1
Build1
1/11/2014
Component 3
Pass
Fail1
City1
Build1
1/06/2014
Component A
Fail
MultiFail
City1
Build1
1/06/2014
Component 1
Fail
MultiFail
City1
Build1
1/06/2014
Component 2
Pass
MultiFail
City1
Build1
1/06/2014
Component 3
Fail
MultiFail
I am looking at Component & Test Result columns, count list of Fails - grouped by Building Name and Date; then generate Calculated result depending on the number of components failed.
If Single component Test Result = fail - then Calculated Result = Fail1
If CountA(components Test Result = fail) <=2 then Calculated Result = Fail2
If CountA(components Test Result = fail) > 2 then Calculated Result = MultiFail
If Component1 AND ComponentA Test Result = Fail then Calculated Result = FailMail
So far, I tried various ways in solving this with a step ahead and 2 steps behind:
I created a calculated column to count # Fails to be used for Calculated Result and struggling to generate Calculated Result.
Tests_Failed =
CALCULATE(COUNT(Table[TestResult]),FILTER(Table,Table[date]=MAX(Table[date])
&& Table[TestResult]="Fail"))
Another way I tried approaching the problem
Calculated Result =
VAR Component = Table[Component]
VAR Date1 = Table[Test date]
VAR Build = Table[Building Name]
RETURN
CALCULATE(DISTINCTCOUNT('Table'[Component]), ALL(Table), FILTER('Table',
'Table'[Test Result]="Fail" && 'Table'[date] = Date1 &&
'Table'[Building Name]=Build)))
Can you try this
Measure =
VAR _1 =
CALCULATE (
CALCULATE (
DISTINCTCOUNT ( 'Table'[Component] ),
FILTER ( 'Table', 'Table'[Test Result] = "Fail" )
),
ALLEXCEPT ( 'Table', 'Table'[Building Name], 'Table'[Test Date] )
)
VAR _2 =
SWITCH (
TRUE (),
ISBLANK ( _1 ) = TRUE (), "None",
_1 = 1, "Fail1",
"MultiFail"
)
RETURN
_2

How to make the agent pick the highest value between two values?

guys. I created this procedure in NetLogo for my agents (farmers):
to calculate-deforestation
ask farmers [
set net-family-labor ( family-labor - ( ag-size * cell-labor-ag-keep ) )
set net-family-money ( family-money - ( ag-size * cell-cost-ag-keep ) )
ifelse net-family-labor < 0 or net-family-money < 0
[ set n-aband-cell-labor ( family-labor / cell-labor-ag-keep )
set n-aband-cell-money ( family-money / cell-cost-ag-keep )
set n-aband with-max [ n-aband-cell-labor n-aband-cell-money ]
]
[ set n-def-cell-labor ( net-family-labor / cell-labor-deforest )
set n-def-cell-money ( net-family-money / cell-cost-deforest )
set n-def with-min [ n-def-cell-labor n-def-cell-money ]
]
]
end
For the "n-aband", I would like to get the max value between "n-aband-cell-labor" and "n-aband-cell-money" (either one or the other; the same goes for "n-def"). I know a limited number of NetLogo primitives but the ones I was able to find do not work for my case, for instance, "with-max", "max-n-of", "max-one-of". I am sure there must be one that would work but I am having trouble finding it in the NetLogo dictionary. I wonder if anyone could suggest me one that could work for my case. Thank you in advance.
If you want to get the max value of a list, simply use max. So,
set n-aband max (list n-aband-cell-labor n-aband-cell-money )
will set n-aband to the highest of the two values.

Misplaced parenthesis in expression

Another pair of eyes to look at this would be great. I get one of two error messages the more I mess with this. Either it want's another parenthesis or that it's not a proper use of True/False. Im just getting a little frustrated looking at it. Any help would be much appreciated. It's been a long day.
=IIF((Sum(Fields!January.Value, "CDataSet")- Code.DivideBy(Fields!M191.Value, Sum(Fields!Totals.Value, "DataSet2")) < 0
and (Sum(Fields!January.Value, "CDataSet")- Code.DivideBy(Fields!M191.Value, Sum(Fields!Totals.Value, "DataSet2")))> Sum(Fields!Variance.Value, "CDataSet")),"Red",
IIF((Sum(Fields!January.Value,"CDataSet")- Code.DivideBy(Fields!M191.Value, Sum(Fields!Totals.Value, "DataSet2"))) > 0
and (Sum(Fields!January.Value, "CDataSet")- Code.DivideBy(Fields!M191.Value, Sum(Fields!Totals.Value, "DataSet2"))) > Sum(Fields!Variance.Value, "CDataSet")), "LimeGreen","Transparent")
When I indent the code to the levels of parentheses, I get:
=IIF(
(
Sum(Fields!January.Value, "CDataSet") - Code.DivideBy(
Fields!M191.Value, Sum(Fields!Totals.Value, "DataSet2")
) < 0
and (
Sum(Fields!January.Value, "CDataSet") - Code.DivideBy(
Fields!M191.Value, Sum(Fields!Totals.Value, "DataSet2")
)
) > Sum(Fields!ThresholdAging.Value, "CDataSet")
),
"Red",
IIF(
(
Sum(Fields!January.Value,"CDataSet") - Code.DivideBy(
Fields!M191.Value, Sum(Fields!Totals.Value, "DataSet2")
)
) > 0
and (
Sum(Fields!January.Value, "CDataSet") - Code.DivideBy(
Fields!M191.Value, Sum(Fields!Totals.Value, "DataSet2")
)
) > Sum(Fields!ThresholdAging.Value, "CDataSet")
),
"LimeGreen",
"Transparent"
)
Conclusion: The inner IIF has only one parameter, and the outer IIF has five. I think that you meant to put the two last strings in the inner IIF:
...
) > Sum(Fields!ThresholdAging.Value, "CDataSet"),
"LimeGreen",
"Transparent"
)
)

Resources