I want to calculate the running count of each value based on column A. In Excel, I am applying the following formula
=COUNTIF($A$2:$A2,A2)
I would like to get the same result in Power BI. Can you please advise.
With just a single column, this is impossible in DAX because duplicated rows cannot be distinguished as there is no inherent order to a column.
However, if you have an index column on the table (you can easily add one in the Query Editor), then it is possible to define such a calculated column so that it works similarly to the Excel formula.
CountIf =
VAR CurrentIndex = DATA[Index]
RETURN
CALCULATE (
COUNTROWS ( DATA ),
ALLEXCEPT ( DATA, DATA[ITEM] ),
DATA[Index] <= CurrentIndex
)
Related
I have a base table 'Orders' with a column OrderNo, this column is being used in a visual.
I have another table 'Operations' with columns: OrderNo, OperationNo, TimeTaken (this is just a numeric column). This table is related to base table with OrderNo. Please note that one OrderNo can have multiple OperationNo.
I want to add a column to my visual 'TimeRemaining', which takes all OperationNo (from Operations table) for an OrderNo and sums the TimeTaken column.
How can I achieve this?
Power bi groups the data in a visual automatically.
if they are linked you can make a calculated column in Orders
TimeRemaining =
SUM ( OperationNo[TimeTaken] )
if you want a it in the Operations specifically then:
TimeRemaining =
CALCULATE (
SUM ( Operations[TimeTaken] ),
ALLEXCEPT ( Operations, Operations[OrderNo] )
)
NOTE.
the first is for a calculated column in Orders table
the second one is for the Operations table.
I want to show the consolidated data for similar record in separate column in power bi data.
I have a data with two columns and i want the result like below in third & fourth column
3rd column result nothing but comparing unique id in rows, e.g. 1=2 =false, 2=2=true
4th column result nothing but concatenation of Value column for unique record
could you please help to achieve this in power bi - i want to create custom columns for these two result in data table
We can use CalculatedColumn in DAX:
Result =
CALCULATE(
CONCATENATEX (
CALCULATETABLE ( VALUES('Unique'[Value] ) ),
'Unique'[Value],
", ",
'Unique'[Value],
ASC
), ALL('Unique'[Value])
)
And you Validation may be a measure:
Valid = if(SELECTEDVALUE('Unique'[Value]) = CALCULATE( max('Unique'[Value]), ALL('Unique'[Value])), FALSE(), TRUE())
I am looking to create column D of the table below in power query or DAX. You can see the excel formula in the cell. I need to add columns from the current row and add a previously calculated row above. The column is self-referencing. I am stuck and do not know how to proceed. Any help would be appreciated.
You cannot recursively self-reference a column in DAX. See here for more detail on that.
That said, just like the examples linked in that post (also below), you don't really need recursion for your purpose but rather a cumulative total of Production - Demand plus an initial inventory amount.
That is, something like this:
Projected Inv. =
CALCULATE (
SUM ( Table1[Production] ) - SUM ( Table1[Demand] ),
FILTER ( Table1[Date], Table1[Date] <= MAX ( Table1[Date] ) )
) + 174408
How to perform sum of previous cells of same column in PowerBI
DAX - formula referencing itself
I have a slicer, called COUNTRY and applied to table MY_TABLE. When I calculate a measure, everything works as expected:
-- calculates distinct count only for COUNTRY = x
Some Measure = DISTINCTCOUNT('MY_TABLE'[SOME_COLUMN])
The problem is SUMMARIZE ignores slicer selection:
-- calculates distinct count accross all countries: x, y, z, etc.
Calculated Table =
RETURN SUMMARIZE(
'SOME_TABLE',
[CATEGORY],
"COUNT", DISTINCTCOUNT('SOME_TABLE'[SOME_COLUMN])
)
How to make SUMMARIZE take into account slicers?
Only Measures are "responsive", calculated tables and columns get calculated and created once, when the data are loaded.
Note that if a calculated table is used inside a measure it will behave correctly, but as you may know, a measure must return a scalar value and not a table. (ie you can use summarize inside a measure, you can then filter the obtained table and return the sum of one column)
Of course, you can filter calculated table with a slicer. If you can, go for SUMMARIZECOLUMNS because this function is better optimized then SUMMARIZE, and has arguments for filtering.
Filtering SUMMARIZECOLUMNS
If you want to stick to SUMMARIZE, you can filter your table by wrapping it with CALCULATETABLE.
Calculated Table =
CALCULATETABLE (
SUMMARIZE (
'SOME_TABLE',
[CATEGORY],
"COUNT", DISTINCTCOUNT ( 'SOME_TABLE'[SOME_COLUMN] )
),
Dim[Color]
= SELECTEDVALUE ( Slicer[SlicerValues] )
)
Should FILTER be used inside or outside of SUMMARIZE?
I have a measure that retrieves the latest bundle string from a column called Message.
The measure works by getting the first single row from SessionEvents and using MAXX to retrieve the Message from that row.
lastBundle =
VAR SINGLE_ROW = TOPN(1,, FILTER(SessionEvents, SessionEvents[StatusId]=4),
SessionEvents[DateTime],DESC)
return MAXX(SINGLE_ROW, [Message])
I want to use this measure in a calculated column but MAXX is not allowed as a part of a calculated column.
How should i change this calculation to work in a calculated column.
You can use SELECTCOLUMNS instead of MAXX as explained more fully in this related Q&A:
Return top value ordered by another column
lastBundle =
SELECTCOLUMNS (
TOPN (
1,
FILTER ( SessionEvents, SessionEvents[StatusId] = 4 ),
SessionEvents[DateTime], DESC
),
"Message", SessionEvents[Message]
)
But I don't see any reason why MAXX shouldn't work too, except your TOPN function has an extra comma.
Edit: I don't think you can do this at all if you are using a DirectQuery. From Microsoft's documentation:
Limitations in calculated columns: Calculated columns are limited to being intra-row, as in, they can only refer to values of other columns of the same table, without the use of any aggregate functions. [...] Functions that are not supported will not be listed in autocomplete when authoring the DAX for a calculated column, and would result in an error if used.