DAX dynamic filter using two columns from related table - ssas

I need to make some simple calculation using DAX, but I am not sure how do I achieve same result as I did in Excel's formulas.
First of all my data structure looks as on screenshot:
Measures are coming from one table, and the PactDelivered is an attribute from related table which is connected by relationship.
All I really have to do is to translate formula as below into DAX:
There is no problem when it comes to calculate B3-C3-D3, but I have a problem how do I calculate the - B4 + C4 part. Of course it should be dynamic, so if it comes to calculate result for Package number 10, then it'd take figures from number 10 and 11.
EDIT:
After modification of code from Alexis Olson I got something like below, but it's not working yet. Please note the PactDelivered is in other table than measures are. Not knowing why, but it's calculating the result on packs according to NextPack value. I tried adding another variable which was basically NextPack -1, and tried calculating the sums from return using it, but didn't work as well.
Cancellations:=
var NextPack = MAX('Order'[PactDelivered]) + 1
var NextOrders = CALCULATE(SUM('Fact DropOff'[OrdersSentOut]) + sum('Fact DropOff'[OrdersReturned]), 'Order'[PactDelivered] = NextPack)
return SUM ('Fact DropOff'[OrdersSentOut]) - SUM ('Fact DropOff'[OrdersReturned] ) - SUM('Fact DropOff'[ActiveSubscriptions]) - NextOrders
EDIT2:
Sample data from fact:
From dimension:
Relationship:

Try a measure like this:
Measure =
VAR NextPact = MAX(Orders[PactDelivered]) + 1
VAR NextOrders = CALCULATE(SUM(Orders[OurdersSentOut]) - SUM(Orders[OrdersReturned]),
Orders[PactDelivered] = NextPact)
RETURN SUM(Orders[OurdersSentOut]) - SUM(Orders[OrdersReturned]) -
SUM(Orders[ActiveSubscriptions]) - NextOrders
First, we calculate the next index by taking the current index and adding 1. Then you calculate sent out minus returns for that index. Finally, subtract that from the current row calculation.

Related

Sum based on different column from model in Power BI

I have data from two tables modelled as follows:
my desired result is:
I've tried SUM(table1[balance],ALLEXCEPT(table1,table1[account])) - doesn't work
this may be simple, but PowerBi is evading me at this point...
Pretty simple to solve it by with extra steps to complete it:
First, on your second table (I named it "cust"), add a new column with the balance from your first table (I named it "acc"):
Balance = LOOKUPVALUE(acc[balance],acc[account ],cust[account])
Cust
Next, I will add a new column to lookupvalue on my "acc" table, follow by adding new column to return the sum value from my "cust" table:
sum = CALCULATE(SUM(cust[Balance]),FILTER(cust,cust[customer ]=EARLIER(acc[customer])))
Acc

Calculate SUM of column based on another column SSRS

I am working on creating a SSRS report that requires me to "have a column that gives me the number for the offer and out of that number how many enrolled." I have attached a screenshot of what I have done so far. I need to figure out how to create an expression (if there is a better way to go about this) that would use following logic: Get SUM of Count column where Enrolled = 'Yes'. Is this possible in SSRS?
Right now your summary expression probably looks something like this
= Sum(Fields!Count.Value)
If so - all you need to do is add a condition include only records with Enrolled = "Yes"
= Sum(IIF(Fields!Enrolled.Value = "Yes", Fields!Count.Value, 0))

Qlikview calculation of range for frequencies

I am given a task to calculate the frequency of calls across a territory. If the rep called a physician regarding the sale of the product 5 times, then frequency is 5 and HCP count is 1....I generated frequencies from 1 to 124 in my pivot table using a calculated dimension which is working fine. But my concern is :
My manager wants frequencies till 19 in order from 1..2..3..4...5..6.....19...
And from the frequency 21-124 as 20+.
I would be grateful if someone helps me with this.....Eager for the reply....
Use the Class function in the dimension, to split into buckets:
=class(CallId,5)
And the expression:
=count(Distinct CallId)
You can then customize the output by adding parameters:
class( var,10 ) with var = 23 returns '20<=x<30'
class( var,5,'value' ) with var = 23 returns '20<= value <25'
class( var,10,'x',5 ) with var = 23 returns '15<=x<25'
I think you can do this with a calculated dimension.
If your data has one row per physician coming from the load statement below will likely work.
Dimension
- =IF(CallCount<=19,CallCount,'+20')
Expression
- =COUNT(DISTINCT Physician_ID)
Sort
- Numeric Value Ascending
If your data has to be aggregated, more than one call row per provider incoming from the load try above substituting below for the Dimension.
Dimension
- =IF(AGGR(SUM(CallCount), Physician_ID) <=19,AGGR(SUM(CallCount), Physician_ID),'+20')

MDX Query SUM PROD to do Weighted Average

I'm building a cube in MS BIDS. I need to create a calculated measure that returns the weighted-average of the rank value weighted by the number of searches. I want this value to be calculated at any level, no matter what dimensions have been applied to break-down the data.
I am trying to do something like the following:
I have one measure called [Rank Search Product] which I want to apply at the lowest level possible and then sum all values of it
IIf([Measures].[Searches] IS NOT NULL, [Measures].[Rank] * [Measures].[Searches], NULL)
And then my weighted average measure uses this:
IIf([Measures].[Rank Search Product] IS NOT NULL AND SUM([Measures].[Searches]) <> 0,
SUM([Measures].[Rank Search Product]) / SUM([Measures].[Searches]),
NULL)
I'm totally new to writing MDX queries and so this is all very confusing to me. The calculation should be
([Rank][0]*[Searches][0] + [Rank][1]*[Searches][1] + [Rank][2]*[Searches][2] ...)
/ SUM([searches])
I've also tried to follow what is explained in this link http://sqlblog.com/blogs/mosha/archive/2005/02/13/performance-of-aggregating-data-from-lower-levels-in-mdx.aspx
Currently loading my data into a pivot table in Excel is return #VALUE! for all calculations of my custom measures.
Please halp!
First of all, you would need an intermediate measure, lets say Rank times Searches, in the cube. The most efficient way to implement this would be to calculate it when processing the measure group. You would extend your fact table by a column e. g. in a view or add a named calculation in the data source view. The SQL expression for this column would be something like Searches * Rank. In the cube definition, you would set the aggregation function of this measure to Sum and make it invisible. Then just define your weighted average as
[Measures].[Rank times Searches] / [Measures].[Searches]
or, to avoid irritating results for zero/null values of searches:
IIf([Measures].[Searches] <> 0, [Measures].[Rank times Searches] / [Measures].[Searches], NULL)
Since Analysis Services 2012 SP1, you can abbreviate the latter to
Divide([Measures].[Rank times Searches], [Measures].[Searches], NULL)
Then the MDX engine will apply everything automatically across all dimensions for you.
In the second expression, the <> 0 test includes a <> null test, as in numerical contexts, NULL is evaluated as zero by MDX - in contrast to SQL.
Finally, as I interpret the link you have in your question, you could leave your measure Rank times Searches on SQL/Data Source View level to be anything, maybe just 0 or null, and would then add the following to your calculation script:
({[Measures].[Rank times Searches]}, Leaves()) = [Measures].[Rank] * [Measures].[Searches];
From my point of view, this solution is not as clear as to directly calculate the value as described above. I would also think it could be slower, at least if you use aggregations for some partitions in your cube.

SQL Report Builder: get value from group within tablix

Please forgive the vague title of this question. Perhaps the below will ask my question better.
Consider the below aggregated table:
Fruit Units FruitSales%
----- ----- -----------
Apples 10 ?
Oranges 20 ?
Bananas 10 ?
NonFruit 10 ?
TOTAL 50 ?
I need the FruitSales% column to be: Fruit / (Total - NonFruit)
If NonFruit is a product name of its own, how do I get its value for use in other calculations in the tablix?
I imagine my formula for the FruitSales% is something like:
Sum(Fields!Units.Value) / (ReportItems!txtTotalUnits.Value - SumIf(Fields!Fruit = "NonFruit", Fields!Units.Value)
However, SumIf does not exist and even if it did, it would be specific to the current row.
And while I'm here, ReportItems!txtTotalUnits.Value, I have obviously named that text box, but is there a cleaner way to reference it?
Say the underlying DataSet (which I've called FruitDataSet) looks like this:
I've created a simple report based on this data:
The Fruit Sales % expression is:
=Sum(IIf(Fields!fruit.Value <> "NonFruit", Fields!units.Value, 0))
/ Sum(IIf(Fields!fruit.Value <> "NonFruit", Fields!units.Value, 0), "FruitDataSet")
This gives what I think is the correct results:
There are two things to note about the expression:
By running the Sum against an IIf expression, you can control what gets included in the totals - here I'm setting NonFruit explicitly to 0.
By setting the Scope of the aggregate expression, you can get overall totals to use to work out total percentages - in the report I'm getting a total using FruitDataSet and comparing this to the group-level total to get a % value.
The way you're referencing the textbox total is fine; the only other option would be to use an expression each time you want the total - if this is outside a Tablix you would need to explicitly set the Scope, e.g. the DataSet.