New to DAX. I need a bit of help when moving excel formulas to DAX. I have this table and call it table1:
Date ID Time
10/06/2020 1 1
10/06/2020 1 2
11/06/2020 1 3
12/06/2020 2 5
13/06/2020 1 6
And I need to calculate count and average per day per ID. The results with two simple formulas (=COUNTIFS(B:B,B2,A:A,A2)) and (=AVERAGEIFS(C:C,B:B,B2,A:A,A2)) in excel is:
Date ID Time Count ifs Avergae ifs
10/06/2020 1 1 2 1.5
10/06/2020 1 2 2 1.5
11/06/2020 1 3 1 3
12/06/2020 2 5 1 5
13/06/2020 1 6 1 6
How can I replicate in DAX?
Update
Count in DAX
DAX Count =
COUNTROWS (
FILTER (
'Table1',
'Table1'[ID] = EARLIER ( 'Table1'[ID] )
&& 'Table1'[Date] = EARLIER ( 'Table1'[Date] )
)
)
Sum per day per ID
Sum =
CALCULATE(
SUM(Table1[Time]),
FILTER(
All(Table1),
Table1[ID] = EARLIER(Table1[ID]) &&
Table1[Date] = EARLIER(Table1[Date])
)
)
Item closed :)
I have answered the question. #Alexis Olson thank you for the inspiration.
Count in DAX
DAX Count =
COUNTROWS (
FILTER (
'Table1',
'Table1'[ID] = EARLIER ( 'Table1'[ID] )
&& 'Table1'[Date] = EARLIER ( 'Table1'[Date] )
)
)
Sum per day per ID
Sum =
CALCULATE(
SUM(Table1[Time]),
FILTER(
All(Table1),
Table1[ID] = EARLIER(Table1[ID]) &&
Table1[Date] = EARLIER(Table1[Date])
)
)
Related
I want to get latest record for each group in a table of rows. ex, i want column c like getting latest record by count
Column A
Column B
Column C
1
09-11-2022 15:46:33
2
1
09-11-2022 21:16:33
4
1
09-11-2022 15:09:40
1
1
09-11-2022 20:39:40
3
2
09-11-2022 15:46:33
1
2
09-11-2022 21:16:33
2
OR
Column A
Column B
Column C
1
09-11-2022 15:46:33
1
09-11-2022 21:16:33
True
1
09-11-2022 15:09:40
1
09-11-2022 20:39:40
2
09-11-2022 15:46:33
2
09-11-2022 21:16:33
True
I want to get flag for latest record in Column C. above mentioned result set i want any of it
thanks in advance
I have tried like this
LastById =
Var modifiedon = 'Table' Column C
Return
COUNTROWS(
FILTER(
ALL( 'Table' ),
'Table' Column C < modifiedon
)
)
The first alternative:
Column C rank =
RANKX (
CALCULATETABLE ( 'Table' , ALLEXCEPT ( 'Table' , 'Table'[Column A] )) ,
[Column B] ,, ASC
)
The second alternative:
Column C bool =
VAR _max =
CALCULATE (
MAX ( 'Table'[Column B] ) ,
ALLEXCEPT ( 'Table' , 'Table'[Column A] )
)
RETURN IF ( [Column B] = _max , "True" )
Column C =
VAR latest = CALCULATE(MAX('Table'[Column B]), ALLEXCEPT('Table','Table'[Column A]))
RETURN IF('Table'[Column B] = latest,"true")
I have table with profit, dev_num
I want to filter that table desc by profit column where running sum of dev_num =<30
i mean sth like this:
Profit | Dev_num
1000000 10
100000 10
340000 6
240000 4
do you know maybe how i should build my measure for this calculated table ?
Your DAX can be like below :
EVALUATE
FILTER (
ADDCOLUMNS (
VALUES ( 'yourTable'[Profit] ),
"Dev_num", CALCULATE ( SUM ( 'yourTable'[Dev_num]) )
),
[Dev_num] <= 30
)
ORDER BY 'yourTable'[Profit]
I have this table:
Order | Total | FirstPayment | Months
1 | 1000 | 2021-01-01 | 2
2 | 600 | 2021-02-01 | 3
And I need to create a another table with the installments, like this:
Month | Order | Value
2021-01-01 | 1 | 500
2021-02-01 | 1 | 500
2021-02-01 | 2 | 200
2021-03-01 | 2 | 200
2021-04-01 | 2 | 200
So, I want to create a child table with one row for each month of payment.
Please, can you help?
As per my comment, I would actually do it like this:
Create dates table that spans all dates between two ranges. You could actually filter it to contain only relevant dates for better performance, but I didn't bother (this is a table formula):
Payments = CALENDAR(MIN(Orders[FirstPayment]), MAXX(Orders, EDATE(Orders[FirstPayment], Orders[Months])))
Create a measure that would show appropriate values for relevant dates:
Payment amount =
SUMX (
Payments,
VAR d =
DAY ( Payments[Date] )
RETURN
SUMX (
FILTER (
Orders,
DAY ( Orders[FirstPayment] )
== d
&& Payments[Date] <= EDATE ( Orders[FirstPayment], Orders[Months] -1 )
&& Payments[Date] >= Orders[FirstPayment]
),
[Total] / [Months]
)
)
The result - based on Order from Orders table and Date from Payments table:
EDIT
Of course, it is also possible to do what you asked. You have to combine the two formulas to create a calculated table like this (below is a table formula that you apply when you select New table):
Installments =
SELECTCOLUMNS (
FILTER (
CROSSJOIN (
CALENDAR (
MIN ( Orders[FirstPayment] ),
MAXX ( Orders, EDATE ( Orders[FirstPayment], Orders[Months] ) )
),
Orders
),
[Date] >= [FirstPayment]
&& DAY ( [Date] ) = DAY ( [FirstPayment] )
&& [Date]
<= EDATE ( [FirstPayment], [Months] - 1 )
),
"Date", [Date],
"Order", [Order],
"Value", [Total] / [Months]
)
I am new to Power BI and the language DAX. Therefore, I find it hard to do simple yearly average across different products.
I have tried a different combination of the functions AVERAGEX and CALCULATE without any luck so far.
My table:
Date | Product | Value
2014-05-06 Cheese 10
2014-08-11 Cheese 12
2015-04-11 Cheese 9
2014-01-22 Milk 4
2014-12-24 Milk 8
The output I try to create:
Date | Product | Value | Yearly_AVG
2014-05-06 Cheese 10 11
2014-08-11 Cheese 12 11
2015-04-11 Cheese 9 9
2014-01-22 Milk 4 6
2014-12-24 Milk 8 6
Lastly, I do not have a calendar table in the dataset.
For a measure, you can write something like this:
YearAvg =
VAR CurrProduct = VALUES ( Sales[Product] )
VAR CurrYear = YEAR ( MAX ( Sales[Date] ) )
RETURN
CALCULATE (
AVERAGE ( Sales[Value] ),
ALLSELECTED ( Sales ),
Sales[Product] IN CurrProduct,
YEAR ( Sales[Date] ) = CurrYear
)
I need to sum each month the ID's amounts that meet the next criterias:
Date End < Agreement date
Date End < Month to show
I have tried with filter, sum and others options, but the result is the same.
My datas are:
ID START DATE AGREEMENT DATE END DATE AMOUNT
1 09/15/2018 01/01/2019 02/20/2019 100
2 09/20/2018 01/15/2019 12/01/2019 100
3 10/01/2018 03/01/2019 12/01/2019 100
4 10/01/2018 03/20/2019 05/01/2019 100
5 11/10/2018 07/10/2019 100
6 09/15/2018 04/05/2019 05/01/2019 100
7 10/01/2018 06/10/2019 05/01/2019 100
8 10/20/2018 07/11/2019 04/10/2019 100
9 11/11/2018 08/01/2019 100
10 11/01/2018 06/01/2019 04/10/2019 100
This is my DAX code:
Result =
CALCULATE (
SUM ( 'Report Diario'[Amount] );
USERELATIONSHIP ( Calendario[Date]; 'Report Diario'[End Date] );
FILTER (
'Report Diario';
'Report Diario'[End Date] < 'Report Diario'[Agreement date]
)
)
finally, I have created a new table and the next calculated column works:
Result = CALCULATE(SUM('Report Diario'[Amount]);FILTER('Report Diario';AND(Calendario[Date]<'Report Diario'[Agreement Date];AND(Calendario[Date]>'Report Diario'[End Date];NOT(ISBLANK('Report Diario'[End Date]))))))