I'm trying to figure out DAX language for Excel and Power BI.
Currently I'm facing a problem when trying to make measurements. I have 2 columns, Resign and Active.
I want to count the rows where the combination is: Resign=1 and active=0.
Do you know how to make this measurement?
Thanks!
Is difficult help you without any data, but if you have the Table 1 for example and the columns that you describe, can do it:
Combination = CALCULATE(COUNTROWS('Table 1'),FILTER('Table 1',AND('Table 1'[Resign]=1, 'Table 1'[Active]=0)))
Please tell me if this help you!
In this case the FILTER-function is not necessary. (referring to the accepted answer). Just using CALCULATE is more efficient. Like so:
Combination =
CALCULATE ( COUNTROWS ( 'Table 1' ); 'Table 1'[Resign] = 1; 'Table 1'[Active] = 0)
Related
I am facing the issue of reusing neat Excel formula for MAXIFs in Power Query M language.
The formula itself consists of several conditions regarding columns in Table2 and a value of interest (VOI) in Table1 (both being Excel table objects).
formula in Table1:
=MAXIFS(Table2[columnA],Table2[columnB],"criteriaB1",Table2[columnC],[#[VOI]],Table2[columnA],"<="&MINIFS(Table2[columnA],Table2[columnB],"criteriaB2",Table2[columnC],[#[VOI]])
(I will divide the formulas into lines to make reading easier)
=MAXIFS(Table2[columnA],
Table2[columnB],"criteriaB1",
Table2[columnC],[#[VOI]],
Table2[columnA],"<="&MINIFS(Table2[columnA],
Table2[columnB],"criteriaB2",
Table2[columnC],[#[VOI]])
So far I've been trying merging Table1 with Table2, grouping by some of the columns but as result I receive chunks of data that I can't/don't know how utilize in next steps. I simply cannot see the complete landscape of the procedure in Power Query M language.
Any help would be appreciated.
The corresponding idea would be to take a maximum over a filtered table.
For example, the MINIFS part would look roughly like this:
MinA =
List.Min(
Table.SelectRows(
Table2, each [ColumnB] = "criteriaB2" and [ColumnC] = "VOI"
)[ColumnA]
)
It gets a bit trickier since you need to pass the current row value of Table1[VOI] into the second condition but it's still doable and might look something like this:
AddMinAColumnToTable1 =
Table.AddColumn(
Table1, "MinA",
(Tab1Row) =>
List.Min(
Table.SelectRows(
Table2, each [ColumnB] = "criteriaB2" and [ColumnC] = Tab1Row[VOI]
)[ColumnA]
)
)
I recommend reading this blog post for a better understanding of the each and (_) => constructions.
Im a new user in Power BI and im having a lot of trouble trying to convert my formulas in excel to work in Power BI.
My main issue is figuring out how to convert IF(AND(OR and several IF's. I have several formulas similar to this one so with the help of converting this one I could most likely do the rest based off this one.
Parameters would be a table in power bi.
E2, Q2, $B$2, etc. would be columns/fields in different tables in power bi.
=IF(E2="";"";IF(AND(OR(E2="Critical";E2="Maximum";E2="Urgent");Q2*24>Parameters!$B$2);"NO";
IF(AND(OR(E2="Alta";E2="High");Q2*24>Parameters!$B$3);"NO";
IF(AND(OR(E2="Media";E2="Medium");Q2*24>Parameters!$B$4);"NO";
IF(AND(OR(E2="Baja";E2="Low");Q2*24>Parameters!$B$5);"NO";
"YES")))))
This formula would be in a new column inside one of the tables.
You already did the work, it's very very similar, I've just added a new column and modify your formula like this:
Column = IF(Table1[E2]="","",
IF(AND(OR(Table1[E2]="Critical",OR(Table1[E2]="Maximum",Table1[E2]="Urgent")),Table1[Q2]*24>Table1[B2]),"No",
IF(AND(OR(Table1[E2]="Alta",Table1[E2]="High"),Table1[Q2]*24>Table1[B2]),"No",
IF(AND(OR(Table1[E2]="Media",Table1[E2]="Medium"),Table1[Q2]*24>Table1[B2]),"No",
IF(AND(OR(Table1[E2]="Baja",Table1[E2]="Low"),Table1[Q2]*24>Table1[B2]),"No","YES"
)
)
)
)
)
Where Table1 is my table, E2,Q2 and B2 are my columns name. About the "Q2*24", because you look like to use always Q2 and not Q2,Q3,Q4,... you can use one VAR instead of a full column with always the same value, like this:
Column =
VAR ReplaceQ2 = 10
RETURN
IF(Table1[E2]="","",
IF(AND(OR(Table1[E2]="Critical",OR(Table1[E2]="Maximum",Table1[E2]="Urgent")),ReplaceQ2*24>Table1[B2]),"No",
IF(AND(OR(Table1[E2]="Alta",Table1[E2]="High"),ReplaceQ2*24>Table1[B2]),"No",
IF(AND(OR(Table1[E2]="Media",Table1[E2]="Medium"),ReplaceQ2*24>Table1[B2]),"No",
IF(AND(OR(Table1[E2]="Baja",Table1[E2]="Low"),ReplaceQ2*24>Table1[B2]),"No","YES"
)
)
)
)
)
Hope this help
=IF(AND(LEFT(L6,3)="CGK",LEFT(M6,3)="CGK"),"Intracity",IF(AND(LEFT(N6,3)="CTC",LEFT(L6,3)=LEFT(M6,3)),"Intracity",IF(AND(LEFT(L6,3)=LEFT(M6,3),LEFT(N6,3)<>"CTC"),"Intercity","Domestik")))
I wanted to calculate the column RestOrder in DAX. Can you guys please help? The TotalOrderDay column was calculated with the Allexcept() formula. I need something like calculate the orders for Statedate remaining or so...or a SQL "rownumber over partition by kinda thing".
Thanks for help!
The following DAX can do:
RestOrder =
COUNTROWS(
FILTER(
'Order',
'Order'[StateDate].[Date] = EARLIER('Order'[StateDate].[Date]) &&
'Order'[StateDate] >= EARLIER('Order'[StateDate])
)
)
Results:
So what you're trying to achieve is to count the number of orders, which are of the same date but is earlier than others.
In order to achieve so, your Statedate has to be in Datetime type.
I have a table(Data_all) that calculates daycount_ytd in one table.
[Date] is in Date Format.
[Fiscal Year] is just year. eg: 2016
Calculated Column
daycount_ytd=DATEDIFF("01/01/"&[Fiscal Year],Data_all[Date],day)+1
Im trying to create a measure that refers to this Calculated Column
Measure:
Amt_X Yield %:=[Amt X]/([Amt Y]/365* (Data_all[DayCount_YTD]))
I get the error that Data_all[DayCount_YTD] refers to a list of values.
How do i filter the expression to get a single value without using a aggregation function eg:(sum, median)?
Or perhaps, is there another way to achieve the same calculation?
You've arrived an a fundamental concept in DAX and once you've worked out how to deal with it then the solution generalises to loads of scenarios.
Basically you can't just pass columns into a DAX measure without wrapping them in something else - generally some kind of mathematical operation or you can use VALUES() depending on exactly what you are trying to do.
This measure will work OK if you use it in a PIVOT with the date as a row label:
=
SUM ( data_all[Amt X] )
/ (
SUM ( data_all[Amt Y] ) / 365
* MAX ( data_all[daycount_ytd] )
)
However you will see it gives you an incorrect total as it is in the latest for the entire thing. What you need is a version that iterates over the rows and then performs a calculation to SUM or AVERAGE each item. There is a whole class of DAX functions dedicated to this such as SUMX, AVERAGEX etc. You can read more about them here
It's not totally clear to me what the maths behind your 'total' should be but the following measure calculates the value for each day and sums them together:
=
SUMX(
VALUES(data_all[date]),
SUM(data_all[Amt X]) /
(SUM(data_all[Amt Y]) / 365 * MAX(data_all[daycount_ytd]))
)
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 :)