This is my requirement and I'm new to DAX, any help would be appreciated:
You can use the AVERAGE function inside of CALCULATE to get what you want.
AvgByEmp = CALCULATE(AVERAGE(RoleTab[Value]), ALL(RoleTab[EmpID]), RoleTab[Value] <> 0)
The ALL(RoleTab[EmpID]) filter removes the EmpID filter context (but keeps the RoleID context) and the RoleTab[Value] <> 0 filter removes the zero values from the calculation.
Related
I have a data set with 10000000 rows with a column ROWNUM that have a 0 or 1. Sometimes the window give the correct answer on order by other times she mix and take the 0 first.
[PK0, PK1, PK2, PK3, PK4, PK5, PK6. PK7, A, B,C,D,E, ROW_NUM]
window = Window.partitionBy(primary_keys).orderBy(desc(job_constants.ROW_NUM))
return df_uniondata.withColumn(job_constants.RANK, rank().over(window)).where(col(job_constants.RANK) == 1)
.coalesce(coalesce_count).select(df_all_changes.columns)
Is window() idempotent?
How can i enforce orderby on rank?
Thanks for the help.
How to convert negative values to zero in a calculated column?
we have inner join of multiple tables where a column is a calculated column taken from tables,i need to convert negative values present in the column to zero, how can i do?
what was tried..
i tried with 'case' condition but its not working.
part of code where i intend to change is similar to like this
(isnull((select isnull(sum(AVAILPHYSICAL),0) from ax.inventsum
where ITEMID = ax.INVENTTABLE.ITEMID ))-
(select isnull(sum(QTY*-1),0) from ax.RETAILTRANSACTIONSALESTRANS RTST inner join ax.RETAILTRANSACTIONTABLE RTT
on RTT.TRANSACTIONID = RTST.TRANSACTIONID
where ITEMID=ax.INVENTTABLE.ITEMID and (RTT.ENTRYSTATUS = 0 OR RTT.ENTRYSTATUS = 2) and
(RTST.TRANSACTIONSTATUS = 0 or
RTST.TRANSACTIONSTATUS = 2 ) and
As Quantity,
id datetime new_column datetime_rankx
1 12.01.2015 18:10:10 12.01.2015 18:10:10 1
2 03.12.2014 14:44:57 03.12.2014 14:44:57 1
2 21.11.2015 11:11:11 03.12.2014 14:44:57 2
3 01.01.2011 12:12:12 01.01.2011 12:12:12 1
3 02.02.2012 13:13:13 01.01.2011 12:12:12 2
3 03.03.2013 14:14:14 01.01.2011 12:12:12 3
I want to make new column, which will have minimum datetime value for each row in group by id.
How could I do it in Power BI desktop using DAX query?
Use this expression:
NewColumn =
CALCULATE(
MIN(
Table[datetime]),
FILTER(Table,Table[id]=EARLIER(Table[id])
)
)
In Power BI using a table with your data it will produce this:
UPDATE: Explanation and EARLIER function usage.
Basically, EARLIER function will give you access to values of different row context.
When you use CALCULATE function it creates a row context of the whole table, theoretically it iterates over every table row. The same happens when you use FILTER function it will iterate on the whole table and evaluate every row against the filter condition.
So far we have two row contexts, the row context created by CALCULATE and the row context created by FILTER. Note FILTER use the EARLIER to get access to the CALCULATE's row context. Having said that, in our case for every row in the outer (CALCULATE's row context) the FILTER returns a set of rows that correspond to the current id in the outer context.
If you have a programming background it could give you some sense. It is similar to a nested loop.
Hope this Python code points the main idea behind this:
outer_context = ['row1','row2','row3','row4']
inner_context = ['row1','row2','row3','row4']
for outer_row in outer_context:
for inner_row in inner_context:
if inner_row == outer_row: #this line is what the FILTER and EARLIER do
#Calculate the min datetime using the filtered rows
...
...
UPDATE 2: Adding a ranking column.
To get the desired rank you can use this expression:
RankColumn =
RANKX(
CALCULATETABLE(Table,ALLEXCEPT(Table,Table[id]))
,Table[datetime]
,Hoja1[datetime]
,1
)
This is the table with the rank column:
Let me know if this helps.
is there a way to set an upper limit to a calculation (calculated field) which is already in a CASE clause? I'm calculating percentages and, obviously, don't want the highest value exceed '100'.
If it wasn't in a CASE clause already, I'd create something like 'case when calculation > 100.0 then 100 else calculation end as needed_percent' but I can't do it now..
Thanks for any suggestions.
I think using least function will be the best option.
select least((case when ...), 100) from ...
There is a way to set an upper limit on a calculated field by creating an outer query. Check out my example below. The inner query will be the query that you have currently. Then just create an outer query on it and use a WHERE clause to limit it to <= 1.
SELECT
z.id,
z.name,
z.percent
FROM(
SELECT
id,
name,
CASE WHEN id = 2 THEN sales/SUM(sales) ELSE NULL END AS percent
FROM
users_table
) AS z
WHERE z.percent <= 1
I am working in Tableau and trying to figure out how to create a filter exclusion. For example I have the following fields.
Hospital CallType CallDate
I want to filter out all hospitals where one of the Calls has a call type of ColdCall and a Call DateBetween X and Y.
I can do this easily in SQL but don't have access to this data in the SQL Database. It would be the following:
Select
Hospital
,CallType
,CallDate
Into
#TempTable
From
Database
Select
Hospital
,CallType
,CallDate
Into
#ExclusionTable
From
Database
Where
CallType = 'Cold'
and
CallDate Between X and Y
Select
Hospital
,CallType
,CallDate
From
#TempTable
Where
Hospital not in
(Select
Hospital
From
#ExclusionTable)
Any suggestions would be greatly appreciated.
Thanks,
Simple. Create a calculated field Filter:
IF CallType = "Cold" AND CallDate < X AND CallDate > Y
THEN 1
ELSE 0
END
Then drag Hospital to filter, go to Condition tab, select by field, get your Filter field, use sum > 0. It will filter out any hospital that have at least one call with your conditions (because all the calls that don't meet will be zero, and if at least one is not zero, the sum will be over 0)
For X and Y, I'd create parameters. It's easier (and safer) than trying to write the dates directly on the field. And you can manipulate then more easily too