I need to perform subtraction and a filter in the same column that I create in Power BI - dax

enter image description here
I need to create a column which subtracts [Retailer_yes_amount] and [classification_base_amount] and at the same time filter out "Not Eligible" category in [Classification] column. [Classification] column has 5 categories - Platinum, Gold, Silver, Bronze and Not eligible.
I was thinking like this New_column = calculate(([Retailer_yes_amount]-[classification_base_amount]),filter('table_name',[classification] <> "Not Eligible")) but it threw an error.
Kindly suggest

If you want to have this evaluated for every row as a new column you have to enter the following expression as a Calculated Column
New_column =
IF(
table_name[Classification] <> "Not Eligible",
[Retailer_yes_amount] - [classification_base_amount]
)
If you want to use a measure you have to specify an aggregation.

Related

How to Select multiple related columns in add calculated fields in Quicksight parameter using ifelse?

I have a parameter 'type' in a table and it can have multiple values as follows -
human
chimpanzee
orangutan
I have 3 columns related to each type in the table -
human_avg_height, human_avg_weight, human_avg_lifespan
chimpanzee_avg_height, chimpanzee_avg_weight, chimpanzee_avg_lifespan
orangutan_avg_height, orangutan_avg_weight, orangutan_avg_lifespan
So if i select the type as human, the quicksight dashboard should only display the three columns -
human_avg_height, human_avg_weight, human_avg_lifespan
and should not display the following columns -
chimpanzee_avg_height, chimpanzee_avg_weight, chimpanzee_avg_lifespan
orangutan_avg_height, orangutan_avg_weight, orangutan_avg_lifespan
I created the parameter type and in the add calculated fields I am trying to use ifelse to select the columns based on the parameter selected as follows -
ifelse(${type}='human',{human_avg_height}, {human_avg_weight}, {human_avg_lifespan},{function})
I also tried -
ifelse(${type}='human',{{human_avg_height}, {human_avg_weight}, {human_avg_lifespan},{function}})
And -
ifelse(${type}='human',{human_avg_height, human_avg_weight, human_avg_lifespan},{function}})
But none of it is working. What am i doing wrong ?
One way to do this would be to use three different calculated fields, one for all the heights, one for weights and one for lifespan. The heights one would look like this:
ifelse(
${type}='human',{human_avg_height}, ifelse(
${type}='chimpanzee', { chimpanzee_avg_height}, ifelse(
${type}='orangutan',{ orangutan_avg_height},
NULL
)))
Make another calculated field for weights and lifespan and then add these calculated fields to your table, and filter by type.
To make it clear to the viewer what data is present, edit the Title of the visual to include the type:
${type} Data
You have to create one calculated field for each measure using the ifelse with the type to choose the correct vale, but is not necessary to create inner ifelse as skabo did, the if else syntax is ifelse(if, then [, if, then ...], else) so you can define the calculated fields as follows:
avg_height = ifelse(${type}='human', {human_avg_height}, ${type}='chimpanzee', {chimpanzee_avg_height},${type}='orangutan', {orangutan_avg_height}, NULL)
avg_weight = ifelse(${type}='human', {human_avg_weight}, ${type}='chimpanzee', {chimpanzee_avg_weight},${type}='orangutan', {orangutan_avg_weight}, NULL)
avg_lifespan = ifelse(${type}='human', {human_avg_lifespan}, ${type}='chimpanzee', {chimpanzee_avg_lifespan},${type}='orangutan', {orangutan_avg_lifespan}, NULL)
Then use those calculated fields in your visuals.

New column indicating if row is the first instance of the value for the Entity ID using SQL instead of DAX

I currently have a column that is created using the following DAX formula (a calculating language used by platforms such as Power BI) which indicates if the listed activity is the first one ever for that Entity ID. Below is my DAX script if it helps at all:
// "Declares column name"
First Time Activity =
// "if the column 'Timestamp' is equal to..."
if('Activity Table'[Timestamp]=
// "...is equal to the earliest Timestamp for that Entity ID and Activity Name"
CALCULATE(min('Activity Table'[Timestamp]),
filter('Activity Table',
'Activity Table'[Entity ID] = earlier('Activity Table'[Entity ID]) &&
'Activity Table'[Activity Name] = earlier('Activity Table'[Activity Name])
)
)
// "...then return a 1. If not, then return a blank/null"
,1,BLANK())
But I need this now to be a column made in PL SQL rather than in DAX. Any help on the SQL script would be much appreciated since I'm fairly novice at SQL.
Thanks
You don't actually need a column. you can write your query as :
Select a
,decode(activity_date
,MIN(activity_date) over (partition by activity_id)
,'Y'
,'N') first_record_indicator
From activity_table a
But, if you table is too huge to actually query like this everytime, you can create a column named first_record_indicator and populate it in "BEFORE INSERT" trigger.
e.g. https://www.techonthenet.com/oracle/triggers/before_insert.php

Power Query (M language) 50 day moving Average

I have a list of products and would like to get a 50 day simple moving average of its volume using Power Query (M).
The table is sorted by product name and date. I add a custom column and applied the code below.
if [date] >= #date(2018,1,29)
then List.Average(List.Range(Source[Volume],[Volume]-1,-50))
else ""
Since it is already sorted by date and name, an if statement was applied with a date as criteria/filter. However, an error occurs that says
'Volume' column not found in the table.
I expect to have an added column in the power query with volume 50 day moving average per product. the calculation to be done if date is greater than or equal Jan 29, 2018.
We don't know what your columns are, but assuming you have [product], [date] and [volume] in Source, this would average the last 50 days of [volume] for the identical [product] based on each [date], and place in a new column
AvgAmountAdded = Table.AddColumn(Source, "AverageAmount", (i) => List.Average(Table.SelectRows(Source, each ([product] = i[product] and [date]<=i[date] and [date]>=Date.AddDays(i[date],-50)))[volume]), type number)
Finally! found a solution.
First, apply Index by product see this post for further details
Then index again without criteria (index all rows)
Then, apply below code
= Table.AddColumn(#"Previous Step", "Volume SMA(50)", each if [Index_byProduct] >= 50 then List.Average(List.Range(#"Previous Step"[Volume], ([Index_All]-50),50)) else 0),
For large dataset, Table.Buffer function is recommended after index-expand step to improve PQ calculation speed

Adding different row values for Birt

I got this selection of data from my sql:
I would like to add Cancelled, Disputed and Resolved together and then divide the result with the total shipped. All of this should be done with an Expression.
So x / 303 where x is the sum of the desired values.
Goal would be to get a % where I can tell how good my shipping is.
I would then like to display the result in a text label next to a graph.
How do I do that?
You should use computed columns in your data set:
Add a SUM on the column Total and a filter only matching the rows based on the column Status you want to select. The expression should look like:
if (row["Status"] == "Cancelled" || row["Status"] == "Disputed"
|| row["Status"] == "Resolved")
true
else
false
create a second computed column only containing the "Total" value where the Status is Shipped.
if (row["Status"] == "Shipped")
row["Total"]
Then create a third computed column where you divide both computed values and you are done.
row["sum"] / row["shipped"]
create a new parameter and refer the image
create new static values and allow multiple values to be selected.
So, accordingly edit your SQL queries

Spotfire Expression Value for Max(Row Count)

I'm trying to make a Calculated Value Control expression on the below columns:
Row Count | Date | Value
What I want to get is the Value for the 'newest' date, which will also be the highest row count. How can I write an expression to get this, it seems like it ought to be simple. I'm having trouble writing it in only expression language, without SQL.
Using the expression below you can limit the records in your table to those with the highest (aka most recent) date which should have your Value of interest.
[Date] = Max([Date])
You can do the same with row count since you mentioned the record of interest being the highest row count:
[Row Count] = Max([Row Count])
If you're looking to create a calculated column you can use a case statement to spit out the value:
case when [Date] = Max([Date]) then [Value] end
Lastly, should you want to display this value in a Text Area to show off your value you can utilize the calculated column above:
1) Create a new Text Area
2) Type some text about what it is: "Value for newest Date: " (optional)
3) Click "Insert Dynamic Item" -> "Calculated Value"
4) Under "Data" ensure the appropriate data table is selected. Note: You can uncheck the "Use Current Filtering..." box here if you do not want your value to update as you filter.
5) Under Values, utilize our calculated column with "Max" wrapped around it to avoid Summing duplicate values:
Max(CALCULATED_COLUMN)
Here is a screenshot of my work with random filler dates and values: http://i.imgur.com/hFapS8c.png
The larger text is to show the calculated value dynamic items. I used Max([Date]) for the date value.

Resources