DAX FILTER FUNCTION - powerpivot

Got two tables in power Pivot (table 1) and (table 2)
Table 1 got a column with Buy/Sell values.
And table 2 got a column called "total value" (which is a currency column)
The tables are connected through a key column (with the exact matching numbers of course)
I want to calculate the the rows with table 1 (Buy) values, against the table 2 column with (total Value).
To summarize I want a measure to know how much is the total value of all the "Buy" and one measure for all the "Sell" values.
tried this formula but it didn't work:
Insider buys Total:=
CALCULATE(
QcomInsider[Total Value];
FILTER('QcomBuyOrSell';
QcomBuyOrSell[Buy/Sell] = "Buy");
(QcomInsider[Total Value])
)
help much appreciated even if I need more than 2 measures

I think you are very close, just use SUM over the right column and you are done.
BUY
Insider buys Total:=
CALCULATE(
SUM(QcomInsider[Total Value]);
FILTER('QcomBuyOrSell';
QcomBuyOrSell[Buy/Sell] = "Buy")
)
SELL
Insider sells Total:=
CALCULATE(
SUM(QcomInsider[Total Value]);
FILTER('QcomBuyOrSell';
QcomBuyOrSell[Buy/Sell] = "Sell")
)

Related

If Statements For Power Pivot

I'm trying to figure out how to calculate my compliance % measures based on if statements.
If [alias]=company A, then the percentage should equal 100%. If it does not, then it should calculate the total complying spend/total overall spend.
However, when I tried to set up the if statement it gives me an error and says that the single value for "alias" column cannot be determined.
I have tried If(Values) statements, but I need it to return more than one value.
Measures always aggregate. The question is what you want the compliance calculation to be when you're looking at 2 companies? 3 companies? Right now, neither your question nor your formula accounts for this possibility at all, hence the error.
If you're thinking "Compliance % doesn't make sense if you're looking at more than one company", then you can write your formula to show BLANK() if there's more than one company:
IF (
HASONEVALUE ( 'Waste Hauling Extract'[Alias] ),
IF (
VALUES ( 'Waste Hauling Extract'[Alias] ) = "company A",
[PCT-Compliant],
[PCT Non-compliant]
),
BLANK ()
)
If you want something else to happen when there's more than one company, then DAX functions like CALCULATE, SUMX or AVERAGEX would allow you to do what you want to do.
The trick with DAX generally is that the formula has to make sense not just on individual rows of a table (where Alias has a unique value), but also on subtotals and grand totals (where Alias does not have a unique value).
Based on your comment that any inclusion of company A results in 100%, you could do something such as:
IF (
ISBLANK (
CALCULATE (
COUNTROWS ( 'Waste Hauling Extract' ),
FILTER ( 'Waste Hauling Extract', 'Waste Hauling Extract'[Alias] = "company A" )
)
),
[PCT Non-compliant],
[PCT-Compliant]
)
The new CALCULATE statement filters the Waste Hauling Extract table to just company A rows, and then counts those rows. If there are no company A rows, then after the filter it will be an empty table and the row count will be blank (rather than 0). I check for this with ISBLANK() and then display either the Non-Compliant or Compliant number accordingly.
Note: the FILTER to just company A only applies to the CALCULATE statement; it doesn't impact the PCT measures at all.

In a pivot table: how to ignore dimension in an expression using a variable that depends on that dimension

I'm trying to rank (A,B,C) a list of customers according to their profitability , which is calculated as the amount of each sale multiplied by the product profitability (each product has a profitability value assigned). Hence, Profit = SaleAmount*ProductProfitability
To rank every customer, I have a pivot table with the customer id (CustID) as dimension and two expressions:
1)
= SaleAmount*ProductProfitability
2) = if(SaleAmount*ProductProfitability > $(vPercentile75Profit),'A', if(SaleAmount*ProductProfitability > $(vPercentil25Profit),'B','C'))
Expression 2) works correctly if I fix the values of vPercentile75Profit and vPercentile25Profit, but obviously I need this to be dynamic.
For that I've defined those variables as (same for both, just switching 0.75 with 0.25):
vPercentile75Profit =Fractile(aggr(sum({$<ProductProfitability = {'>0'} >} SaleAmount*ProductProfitability/100),CustID), 0.75)
If I understand well, this calculates a list of each customer profitability and then performs the 75 percentile of that list (which is a single value). This works great if I show the value in a Text box for example, however, if I use it in my table, it takes a different percentile for each customer (since CustID is in the dimension).
How can I bypass this? The percentiles must be the same for each customer, but I cannot find the way.
Thanks in advance, any help will be greatly appreciated!
Nothing works better to find the answer than asking your question to others. It was as simple as adding TOTAL to the variable definition:
vPercentile75Profit =Fractile(TOTAL aggr(sum({$<ProductProfitability = {'>0'} >} SaleAmount*ProductProfitability/100),CustID), 0.75)

Power Pivot / DAX - Add column that flags all entries for customers who meet criteria in any of their rows

I have a large database (2 million rows). There are many columns but the two relevant ones are CustomerID and AccountType. A customer can have more than one account type and if they do this will show as different rows in the database. If any of a customers accounts are AccountType = Premium, then they are a Premium customer.
I want to add a column in PowerPivot that will state whether a customer is Premium or not. So for example:
CustomerID Account Type Custom Column
1 Basic Premium
2 Deposit Not Premium
3 Savings Not Premium
1 Premium Premium
So in my example because customer 1 has a Premium account in the last row, the first row is also flagged as Premium. To make it one step trickier, there are actually a few codes, so it could be Premium1, Premium2 etc.
I think I could do this by creating a separate table and linking the two, but I would prefer to avoid this step if possible to keep the file size down.
Try this in the expression for the calculated column:
Custom Column =
IF (
COUNTROWS (
FILTER (
Table,
[CustomerID] = EARLIER ( Table[CustomerID] )
&& [Account Type] = "Premium"
)
)
> 0,
"Premium",
"Not Premium"
)
It is not tested but should work, let me know if this works for you.

SQL: Calculate Percentage in new column using another column

I found it hard to describe what I wanted to do in the title, but I will be more specific here.
I have a reasonably long query:
SELECT
/*Amount earned with validation to remove outlying figures*/
Case When SUM(t2.[ActualSalesValue])>=0.01 OR SUM(t2.[ActualSalesValue])<0 Then SUM(t2.[ActualSalesValue]) ELSE 0 END AS 'Amount',
/*Profit earned (is already calculated then input into db, this just pulls that figure*/
SUM(t2.[Profit]) AS 'Profit',
/*Product Type - pulls the product type so that we can sort by product*/
t1.[ucIIProductType] AS 'Product Type',
/*Profit Percentage - This is to calculate the percentage of profit based on the sales price which uses 2 different columns - Case ensures that there are no wild values appearing in the reports as previously experienced*/
Case When SUM(t2.[ActualSalesValue])>=0.01 OR SUM(t2.[ActualSalesValue])<0 THEN (SUM(t2.[Profit])/SUM(t2.[ActualSalesValue])) ELSE 0 END AS 'Profit Percentage',
/*Percentage of Turnover*/
*SUM(t2.[ActualSalesValue])/(Select SUM(t2.[ActualSalesValue]) OVER() FROM [_bvSTTransactionsFull]) AS 'PoT'
/*The join is connect the product type with the profit and the amount*/
FROM [dbo].[StkItem] AS t1
INNER JOIN [dbo].[_bvSTTransactionsFull] AS t2
/*There attirbutes are the links between the tables*/
ON t1.[StockLink]=t2.[AccountLink]
WHERE t2.[TxDate] BETWEEN '1/Aug/2014' AND '31/Aug/2014' AND ISNUMERIC(t2.[Account]) = 1
Group By t1.[ucIIProductType]
The 'Percentage of Turnover' part I am having trouble with - I am trying to calculate the percentage of the Amount based on the total amount - using the same column. So eg: I want to take the Amount value in row 1, then divide it by the total amount of the entire column and then have that value listed in a new column. But I keep getting errors or I Keep getting 1 (because it wants to divide the value by the same value. CAN anyone please advise me on proper syntax for solving this:
/*Percentage of Turnover*/
*SUM(t2.[ActualSalesValue])/(Select SUM(t2.[ActualSalesValue]) OVER() FROM [_bvSTTransactionsFull]) AS 'PoT'
I think you want one of the following:
SUM(t2.[ActualSalesValue])/(Select SUM(t.[ActualSalesValue]) FROM [_bvSTTransactionsFull] t) AS PoT
or:
SUM(t2.[ActualSalesValue])/(SUM(SUM(t2.[ActualSalesValue])) OVER() ) AS PoT
Note: you should use single quotes only for string and date constants, not for column and table names. If you need to escape names, use square braces.

Filtering a Measure (or Removing Outliers)

Say I have a measure, foo, in a cube, and I have a reporting requirement that users want to see the following measures in a report:
total foo
total foo excluding instances where foo > 10
total foo excluding instances where foo > 30
What is the best way to handle this?
In the past, I have added Named Calculations which return NULL if foo > 10 or just foo otherwise.
I feel like there has to be a way to accomplish this in MDX (something like Filter([Measures].[foo], [Measures].[foo] > 10)), but I can't for the life of me figure anything out.
Any ideas?
The trick is that you need to apply the filter on your set, not on your measure.
For example, using the usual Microsoft 'warehouse and sales' demo cube, the following MDX will display the sales for all the stores where sales were greater than $2000.
SELECT Filter([Store].[Stores].[Store].members, [Unit Sales] > 2000) ON COLUMNS,
[Unit Sales] ON ROWS
FROM [Warehouse and Sales]
I met similar problem when use saiku (backend with Mondrain), as I haven't found any clear solution of "add filter on measure", I added it here, and that may be useful for other guy.
In Saiku3.8, you could add filter on UI: "column"->"filter"->"custom", then you may see a Filter MDX Expression.
Let's suppose we want clicks in Ad greater than 1000, then add the following line there:
[Measures].[clicks] > 1000
Save and close, then that filter will be valid for find elem with clicks greater than 1000.
The MDX likes below (suppose dt as dimension and clicks as measure, we want to find dt with clicks more than 1000)
WITH
SET [~ROWS] AS
Filter({[Dt].[dt].[dt].Members}, ([Measures].[clicks] > 1000))
SELECT
NON EMPTY {[Measures].[clicks]} ON COLUMNS,
NON EMPTY [~ROWS] ON ROWS
FROM [OfflineData]
i think you have two choices:
1- Add column to your fact(or view on data source view that is based on fact table)like:
case when unit_Price>2000 then 1
else 0
end as Unit_Price_Uper_Or_Under_10
and add a fictitious Dimension based on this columns value.
and add named query for New Dimension(say Range_Dimension in datasourceview :
select 1 as range
union all
select 0 as range
and after taht you cant used this filter like other dimension and attribute.
SELECT [Store].[Stores].[Store].members ON COLUMNS,
[Unit Sales] ON ROWS
FROM [Warehouse and Sales]
WHERE [Test_Dimension].[Range].&[1]
the problem is for every range you must add When condition and only if the range is static this solution is a good solution.
and for dynamic range it's better to formulate the range (based on disceretizing method )
2- add dimension with granularity near fact table based on fact table
for example if we have fact table with primary key Sale_id.we can add
dimension based on fact table with only one column sale_Id and in dimension Usage tab
we can relate this new dimension and measure group with relation type Fact and
after that in mdx we can use something like :
filter([dim Sale].[Sale Id].[Sale Id].members,[Measures].[Unit Price]>2000)