How to count cumulatively count occurrences in Power BI? - powerbi

Let's say I have a table like this:
column1 Column2
A 01/01/2020
B 01/01/2020
C 04/01/2020
A 07/01/2020
B 07/01/2020
A 12/01/2020
C 10/01/2020
What I am trying to do is count how many times a value in column1 has occurred. So I want to be able to end up with this:
column1 Column2 column3
A 01/01/2020 1
B 01/01/2020 1
C 04/01/2020 1
A 07/01/2020 2
B 07/01/2020 2
A 12/01/2020 3
C 10/01/2020 2
I found DAX to count how many times in total a value occurs but I can't seem to find how to count them cumulatively. Thanks in advance.

The logic is the same as with a cumulative sum, just with COUNT instead:
column3 =
CALCULATE (
COUNT ( Table1[column1] ),
ALLEXCEPT ( Table1, Table1[column1] ),
Table1[Column2] <= EARLIER ( Table1[Column2] )
)
See also:
Cumulative Total DAX Pattern
Cumulative sum on different columns grouped by date and filtered differently

Related

create a measure count of values and blank in a column with dax

I have these columns and I want to create a measure to calculate the number of types
types
ID Type
1 A
2 A
3 B
4 C
5 D
6 A
7
8
Results :
A 3
B 1
C 1
D 1
Blank 2
Try this Calculated Table:
Results =
SUMMARIZE(
'Table',
'Table'[Type],
"Count", COUNT('Table'[ID])
)

DAX | Sum up rows for a column based on conditions in another column

I am new to DAX, I have a data which looks like -
col1
col2
A
20
A
10
B
30
B
20
My output should be -
col1
col2
col3
A
20
30
A
10
30
B
30
50
B
20
50
I have tried writing a measure but it dosnt work -
col3 =
CALCULATE (
SUM ( Sheet1[col2] ),
FILTER (
ALLSELECTED ( Sheet1 ),
Sheet1[col1] == Sheet1[col1]
)
)
If you are actually looking for a formula to calculate col3, take this one here and put all cols into a table visual.
col3 =
CALCULATE(
SUM('Sheet1'[col2]),
ALLEXCEPT(
'Sheet1',
'Sheet1'[col1]
)
)

DAX formula for sales sum for each day of previous n days

Given a table FACT in Power BI with three columns Date, Category and Sales I am looking for a DAX function that for each day returns the sum of the sales of its previous n days.
Lets assume n = 2, that means for day 01/04/2020 my measure should return the sum of the sales of the days 01/02/2020 and 01/03/2020.
Here is a small example:
Date Category Sales
01/01/2020 A 1
01/01/2020 B 3
01/02/2020 B 2
01/03/2020 B 1
01/04/2020 A 0
01/05/2020 B 10
01/06/2020 B 7
What I want is
Date MyMeasure
01/01/2020 0
01/02/2020 4
01/03/2020 6
01/04/2020 3
01/05/2020 1
01/06/2020 10
I later would then like to use the Category as a filter and in my case n is 365.
I tried the following
MyMeasure =
VAR FROM_DATE =
DATEADD ( FACT[DATE], 0, DAY )
VAR SALES_365 =
CALCULATE (
SUM ( FACT[SALES] ),
DATESINPERIOD ( FACT[DATE], FROM_DATE, 365, DAY )
)
RETURN
SALES_365
but got an error that a table of multiple values was supplied where a single value was expected.
The error was in the variable FROM_DATE, in which DATEADDD returned a column of dates and not a single value.
That said you don't really need a variable for that, you can just use a simple subtraction and DATESBETWEEN(DateCol, DateFrom,DateTo)
MyMeasure =
CALCULATE (
SUM ( FACT[SALES] ),
DATESBETWEEN('FACT'[DATE],MAX(FACT[DATE])-2,MAX(FACT[DATE])-1)
)

Calculate rolling average and use for missing values

I am trying to build a model in PowerBI that forecasts future values based certain inputs. The issue I am running into is that some of my data is missing rows, and I also need to use the last value of data as the one for the next period, to which I will add additional (calculated) values. I used an index that starts at -5 and goes through 30, and added a date column expressed as Today()+Index. The data looks like this:
Index | Date Index | Values |
------------------------------
-6 11/4 2
-5 11/5 5
-4 11/6 7
-3 11/7
-2 11/8 5
-1 11/9 4
0 11/10 <-- This is today
1 11/11
2 11/12
...
What I need the data to look like is this:
Index | Date Index | Values |
------------------------------
-6 11/4 2
-5 11/5 5
-4 11/6 7
-3 11/7 4.667
-2 11/8 5
-1 11/9 4
0 11/10 4 <-- This is today
1 11/11 4
2 11/12 4
...
The Dax Formula I have is:
Values =
If(
AND(
SUMX(
Filter(
Table 2,Table 2[Date]='Summary Table'[Date Index]
),
Table 2[Initial Values]
)=0,
'Summary Table'[Date Index]<Today()
),
Calculate(
Averagex(
Table 2,Table 2[Initial Values]
),
DATESINPERIOD(
Table 2[Date],Today()-1,5,DAY
)
),
SUMX(
Filter(
Table 2,Table 2[Date]='Summary Table'[Date Index]
),
Table 2[Initial Values]
)
)
Note that I am pulling in my values from a different table, Table 2, which has multiple dates with values, which I am summing in this summary table by date. For some reason, the formula is filling in an incorrect average value for missing values, and not pulling the last value (from Today()-1) through to the end of the data. Any help or advice would be appreciated!

How to calculate yearly average per product?

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
)