How do I create a measure in Power Pivot that pulls a value from another table? - excel

I have two tables that use a unique concatenated column for their relationship. I simply want to make a measure that uses the values from C4 of Table1. I thought I could use a simple formula like =values(Table1[C4]) but I get an error of "A table of multiple values was supplied where a single value was expected."
Side note: I realize the concatenation is unnecessary here in this simple example, but it is necessary in the full data I am working with which is why I added it into this example.
Here's a simplified set of tables for what I am trying to do:
Table1
Table2
Relationships

First you should think. Do I really need a Calculated column? Can't this be calculated at runtime?
But if you still want to do it, you can use RELATED or RELATEDTABLE.
Keep in mind if you are pulling from RELATEDTABLE, returns many values. So you should apply some type of aggregation like SUMX or MAXX.

You can use context transition to retrieve the value.
= CALCULATE(MAX(Table1[C4])

Related

How do I use XL Data Tables with XLOOKUP for Data Validation

I have a data table based on a query -> tblPrimaryProducts
tblPrimaryProducts has many columns, some of which are Category, SubCategory and Item.
If I were to use a filter on Category I would of course be able to see the SubCategories and Items in that Category. Likewise if I were to apply a filter to SubCategory in turn I would see a list of on the items in that specific Category + SubCategory selection. Ultimately this is the goal of data validation I am trying to implement.
There is a data entry table -> tblPackages
tblPackages has many fields, but importantly the three listed above need to be implemented in a controlled way via data validation so that each row can have the correctly selected Category + SubCategory + Item
I am unable to change the data table into an array or simple (or dynamic) named range because it will be refreshed any number of times and the column positions will potentially alter. This means that I have to use the table based naming system for both tables.
What I have so far is the following for a data validation formula using XLOOKUP:
XLOOKUP([#[Primary Category]] & [#[Primary SubCategory]], tblPrimaryProducts[Category] & tblPrimaryProducts[SubCategory], tblPrimaryProducts[Item],,0)
[#[Primary Category]] and [#[Primary SubCategory]] are fields in the tblPackages data entry table. The other fields listed come from tblPrimaryProducts. So far I have entered data by hand to test this out.
In the XL Formula window (where you can 'see' what your formula is doing while you fill in the fields) this seems to work and provides the output expected.
When I try to implement this as data validation it complains about an error in formula. If I place an INDIRECT at the beginning I get the syntax error popup. I have had issues in the past with INDIRECT that have been solved with the careful use of quotation marks. I cannot seem to find a way here to do this.
Does anyone have any ideas on how I might implement some data validation based off of a data table (as opposed to an array), or the query that the table is based on in this situation? Under normal circumstances I would use dynamic named ranges but here I cannot.
Any help or ideas greatly appreciated
In Office365 the following formula will get you the list of "Items" that satisfy the "Category" and "Subcategory" conditions.
=FILTER(tblPrimaryProducts[Item],(tblPrimaryProducts[Category]=[#[Primary Category]])*(tblPrimaryProducts[Subcategory]=[#[Primary Subcategory]]),"")
It will give you a #SPILL! error if inserted in a Table and it returns multiple items that match the two criteria. You may have to redesign your output table.
If you are intending to display just the filtered values, you can then use two dropdowns with list of categories and subcategories. The filter function can then use these cells to filter the Items from source data and display as a list below.

How to get value from nested relations in Power Pivot?

I'm using Power Pivot add-in to create a data warehouse for generate dynamic tables and graphs (strictly data source is Excel), but I have a problem whit a calculate in the relations. My data model is the following:
My Snowflake data warehouse model
So for the fact table "fSales" I need to multiply the dCostDetail[Value] per dWorkCost[Value] to generate the fSales[Expenses] amount.
I tried to use the formula but I get an error: related but it don't allow to nested between the relations, e.g. fSales[Expenses] = related(dCostDetail[Value])*related(dWorkCost[Value])
Also I tried to use the next formula:
fSales[Expenses] = related(dWorkCost[Value]) * Calculate(Calculate(Calculate(Value(dCostDetail[Value]), Userelationship(fSales[IdProduct],dProduct[Sku]),Userelationships(dProduct[IdCateg],dCategory[IdCategory]), Userelationships(dCategory[IdCategory],dCostDetail[IdCateg]))))
And I need this "type" of normalized model to have the details when I analyze the information, e.g. filter, but if you know another way to generate the calculation it would be ok.
RELATED doesn't work in measures, because it evaluates on a record-by-record level. So you're on the right track, but what you need to do is create a column in Powerpivot in the fSales table called "Cost Detail" or whatever, and use a RELATED formula there to pull in that value from the CostDetail table. Create another column and do the same thing to pull in the dWorkCost value into the fSales table.
Then you can do a measure for the expenses like this:
Expenses:=SUM([whatever you called CostDetailColumn])*SUM([whatever you called WorkCostColumn])
You should be able to drop that measure into a pivot and it should do what you're looking for.

Avoid DISTINCTCOUNT in PowerPivot

Due to performance issues I need to remove a few distinct counts on my DAX. However, I have a particular scenario and I can't figure out how to do it.
As example, let's say one or more restaurants can be hired at one or more feasts and prepare one or more menus (see data below).
I want a PowerPivot table that shows in how many feasts each restaurant was present (see table below). I achieved this by using distinctcount.
Why not precalculating this on Power Query? The real data I have is a bit more complex (more ID columns) and in order to be able to pivot the data I would have to calculate thousands of possible combinations.
I tried adding to my model a Feast dimensional table (on the example this would only be 1 column of 2 rows). I was hoping to use that relationship to be able to make a straight count, but I haven't been able to come up with the right DAX to do so.
You could use COUNTROWS() combined with VALUES().
Specifically, COUNTROWS() will give you the count of rows in a table. That means COUNTROWS is expecting a table is input. Here's the magic part: VALUES() will return a table as results, and the table it returns are the distinct values in the table/column that you provide as the argument for VALUES().
I'm not sure if I'm explaining it well, so for the sample data you provided, the measure would look like this (assuming the table is named Table1):
Unique Feasts:=COUNTROWS(VALUES('Table1'[Feast Id]))
You can then create a pivot table from Powerpivot, and drag Restaurant Id into Rows, and drag the measure above into Values. Same result as DISTINCTCOUNT, but with less performance overhead (I think).

Excel Powerpivot/Slicer: Dynamically choose chart variable

I just started using powerpivot.
Currently I have slicers that filter based on the values of one variable, but I also want to create a pivotchart where the user can select what variable to graph. Is there a way to populate one slicer with variables from a powerpivot table?
I have a handful of variables that a user might want to look at, and I do not want to have one graph for each variable.
I hope this makes sense. Any suggestions? Thanks & Cheers.
There is a solution to this that is pretty straightforward and although it's not perfect as it doesn't work dynamically, I've used it very successfully in this kind of situation where you are talking about selecting between a well defined set of metrics.
The technique makes use of a disconnected table to drive the slicer with the final formula using the selected value to decide which measure to use.
Lets say you have 2 measures [Sales] and [Profit] you want to choose between. You create a table in your PowerPivot model called, say, 'SlicerTbl' with a single column called Measure that has 2 rows 'Sales' and 'Profit'. No relationship is required. (the table can be called whatever you like btw).
You can then write a 3rd measure that enables you to choose which measure to use:
=
IF (
HASONEVALUE ( SlicerTbl[Measure] ),
SWITCH (
VALUES ( SlicerTbl[Measure] ),
"Measure 1", [Measure 1],
"Measure 2", [Measure 2]
),
BLANK ()
)
This basically checks to see if the slicer has a single value and if so uses VALUES() to return the selected value - because of the initial clause this can only ever be a single row. This means it returns blank in the case of multiple selections.
The SWITCH() is basically an elegant IF() that is easy to scale so I don't see why you couldn't do this with 20+ measures very quickly.
Hope this helps, I created a stupidly simple example here: Example Model

How do I SUMIF one PowerPivot table according to the rows of a second PowerPivot table?

I have two tables: one of customers ("Donor"), and one of transactions ("Trans"). In Donor, I want a "Total" column that sums all the transactions by a particular Donor ID, which I would calculate in a standard Excel table thus:
=SUMIF(Trans[Donor ID],[#ID],Trans[Amt])
Simple! How do I do the same thing with a DAX formula? I thought
=CALCULATE(SUM(Trans[Amt]),Trans[Donor ID]=[ID])
would do it, but I get the error
Column "ID" cannot be found or may not be used in this expression.
Strangely, when I use
=CALCULATE(SUM(Trans[Amt]),Trans[Donor ID]=3893)
I do get the total for ID 3893.
Eschewing CALCULATE, I did find that this works:
=SUMX(FILTER(Trans, Trans[Donor ID]=[ID]),[Amt])
...but it only allows the one filter, and I'll need to be able to add more filters, but:
=SUMX(CALCULATETABLE(Trans, Trans[Donor ID]=[ID]),[Amt])
...(which I understand is like FILTER but allows for multiples) does not work.
Can you identify what I'm doing wrong?
After putting together a quick model that looks like this:
I've confirmed that this DAX forumla works as a Calculated Column in the Donor table:
=CALCULATE(SUM(Trans[Amt]), FILTER(Trans, Trans[Donor] = Donor[DonorKey]))
The key here is to make sure that the relationship between the two tables is correctly configured, and then make sure to use the combination of CALCULATE() and FILTER() -- filtering the trans table based on the current donor context.

Resources