# PowerPivot Calculated Fields Across Multi Tables + Relationship Creation



## Mfrosty (Dec 18, 2012)

Happy Holidays Mr.Excel Forums!

The only present I want from Santa this year is the answer to these two problems.. Lame jokes aside, let's begin..

I am creating a dashboard with PowerPivot that is being driven from our MySQL db. Due to issues with the odbc connection driver I decided to store the data locally into excel files and connect powerpivot to that.

My two queries produce the following table structure in PowerPivot:

Table1 -- No unique Columns/Fields

DateWeekMonthQuarterYearClientLine ItemLI IDLI TypeLI PayoutTierImpressionsClicksSpent12/1/12481242012DogBonez5CPCClicks22,564,15226,45412,382.9912/1/12481242012CatFishez6CPCClicks1315,56414,5489,549.9812/2/12481242012DogBonez5CPCClicks21,489,11214,9548,464.5712/2/12481242012CatFishez6CPCClicks1545,97217,3619,834.11

<tbody>

</tbody>

Table2 -- No unique Columns/Fields

Line ItemLI IDStart DateEnd DateBudgetPayoutPayout PercentFeeGoalBonez52/2/1212/3/12$600,000$1.50$5,0001,000,000Fishez68/9/1212/31/12$400,000$1.12800,000Bonez512/3/123/1/13$200,000$1.40300,000Purple72/14/135/1/13$100,000.12$5,000150,000

<tbody>

</tbody>

Table3 -- Unique lookup table to create a relationship between 1 and 2

LI ID567

<tbody>

</tbody>


So that's what they look like. Query 1 groups by day & query 2 groups by almost all the fields to capture when a change in a line item occurs (new date, new budget, etc.)

*Onto my 1st question..*

I tried to create several calculated fields and was being met with this lovely error at every corner:


```
Calculation error in measure 'Table1'[Events to Date]: The value for column 'LI Type' in table 'line_item_total' cannot be determined in the current context. Check that all columns referenced in the calculation expression exist, and that there are no circular dependencies. This can also occur when the formula for a measure refers directly to a column without performing any aggregation--such as sum, average, or count--on that column. The column does not have a single value; it has many values, one for each row of the table, and no row has been specified.
```

For example I was trying to create a calculated field that would show the "events to date":


```
=IF(Table1[LI Type]= "percent", sum(daily[Spent])*(1+Table2[Payout Percentage]),IF(Table1[LI Type]= "CPC", sum(Table1[Clicks]), if(Table1[LI Type]= "CPF", sum(Table1[Connections]))))
```

I was reading through misc. forums about creating a new calculated field for each calculation first and wrapping it in 'CALCULATE()', but I didn't have any luck with that either. What am I missing here?



*Onto my 2nd question..
*
What additional relationships or tables do I need to add so that I can create a PivotTable that combines Tables 1 & 2?
Example:

ClientLI IDLI TypeLI PayoutTierBudgetPayoutPayoutPercentFeeGoalStartDateEndDateImpressionsClicksSpentDog5CPCClicks2200,0001.40300,00012/3/123/1/131,489,11214,9548,464.57

<tbody>

</tbody>
And ultimately be able to splice this data by the Table 1 date field so I can see the line item progress over time.

Some material I read that "may" have my answer hidden in it: User-Friendly Report Sorting With Slicers! « PowerPivotPro

Sorry for the long post, and thanks in advance for any help/insights!


----------



## miguel.escobar (Dec 18, 2012)

I'm pretty sure that this thread will help you A LOT
http://www.mrexcel.com/forum/powerpivot-questions/673511-linked-table-info-used-calculate-column.html

It's pretty much the same scenario. I'm working on a scenario so I can upload a file as well later.

Happy holidays!
Miguel


----------



## miguel.escobar (Dec 18, 2012)

Man...This is probably the fastest I've answered a post in my life...

Check out that your DIM table (the one with the Id's for LI) is missing the TYPE column to show if it's for percent or if there's a CPC/CPF for it so I've created that column and put on some values in it. 
What I did was:
 - Act as Santa for 2 minutes while wearing a xmas hat
 - Created calculated tables on the fact table to show the corresponding %percent and fee

Here's the file: https://dl.dropbox.com/u/54063091/Case.xlsx

I'm missing the:

=IF(Table1[LI Type]= "percent", sum(daily[Spent])*(1+Table2[Payout Percentage]),IF(Table1[LI Type]= "CPC", sum(Table1[Clicks]), if(Table1[LI Type]= "CPF", sum(Table1[Connections]))))

as I think that you should create a new calculated column instead of a measure (easy way) so you can get a column of the aggregate that you're trying to come up with.

Let me know if this xmas present is good enough! There's still time as Santa's still wearing the hat 

Miguel


----------



## Mfrosty (Dec 18, 2012)

Thank you so much! Let me read through these and see what you did, but I hope this is the solution I have been looking for! Thanks Santa!

Edit: Just read through your calculations and they look spot on! 

So just to re-cap: I basically want to just go through and create new calculatedcolumns in table 1 to bring over the values from table 2 after I have created a relationship between the two using table 3? Thank you!


----------



## Mfrosty (Dec 18, 2012)

Sorry still having an issue:

"Calculation error in column 'daily'[]: A table of multiple values was supplied where a single value was expected."


edit: Nevermind mystery solved. Was missing a relationship! Thanks Santa!


----------



## miguel.escobar (Dec 18, 2012)

I was missing the last part but now you made it crystal clear! Instead of me doing the final solution, why don't you try using the columns now that you have them available in 1 table. 


> Give a man a fish and you feed him for a day. Teach a man to fish and you feed him for a lifetime.



In the event that you encounter any problems just post it here.

Best!
Miguel


----------



## miguel.escobar (Dec 18, 2012)

Mfrosty said:


> Sorry still having an issue:
> 
> "Calculation error in column 'daily'[]: A table of multiple values was supplied where a single value was expected."
> 
> ...



Awesome! glad it worked.

Happy holidays!
Miguel


----------



## Mfrosty (Dec 18, 2012)

Santa don't go just yet!

One more question. I am getting a calculation error when I try to migrate budget over. Here is my formula:

=CALCULATE(VALUES(payout_table[Budget]),FILTER(RELATEDTABLE(payout_table), [Date]>=payout_table[Start Date]),FILTER(RELATEDTABLE(payout_table), [Date]<=payout_table[End Date]))

Error: Calculation error in column 'Table1'[]: A table of multiple values was supplied where a single value was expected.

I assume I need to filter by something else to find a specific budget amt unique to a LI id?


----------



## Mfrosty (Dec 18, 2012)

So I switched the VALUES() function to SUM() and it seems to work. It does product duplicates, but I can work around that.


----------



## miguel.escobar (Dec 18, 2012)

Hey frosty!

I'm on my phone right now but it seems that you are right, there are duplicates and that's why it can't give you just 1 distinct value in return. Could you upload the workbook with the formula that you are trying and what you're trying to accomplish?

Not the file that I created but your actual model so we can work with all the dependents.

Best!
Miguel a.k.a. Santa


----------



## Mfrosty (Dec 18, 2012)

Happy Holidays Mr.Excel Forums!

The only present I want from Santa this year is the answer to these two problems.. Lame jokes aside, let's begin..

I am creating a dashboard with PowerPivot that is being driven from our MySQL db. Due to issues with the odbc connection driver I decided to store the data locally into excel files and connect powerpivot to that.

My two queries produce the following table structure in PowerPivot:

Table1 -- No unique Columns/Fields

DateWeekMonthQuarterYearClientLine ItemLI IDLI TypeLI PayoutTierImpressionsClicksSpent12/1/12481242012DogBonez5CPCClicks22,564,15226,45412,382.9912/1/12481242012CatFishez6CPCClicks1315,56414,5489,549.9812/2/12481242012DogBonez5CPCClicks21,489,11214,9548,464.5712/2/12481242012CatFishez6CPCClicks1545,97217,3619,834.11

<tbody>

</tbody>

Table2 -- No unique Columns/Fields

Line ItemLI IDStart DateEnd DateBudgetPayoutPayout PercentFeeGoalBonez52/2/1212/3/12$600,000$1.50$5,0001,000,000Fishez68/9/1212/31/12$400,000$1.12800,000Bonez512/3/123/1/13$200,000$1.40300,000Purple72/14/135/1/13$100,000.12$5,000150,000

<tbody>

</tbody>

Table3 -- Unique lookup table to create a relationship between 1 and 2

LI ID567

<tbody>

</tbody>


So that's what they look like. Query 1 groups by day & query 2 groups by almost all the fields to capture when a change in a line item occurs (new date, new budget, etc.)

*Onto my 1st question..*

I tried to create several calculated fields and was being met with this lovely error at every corner:


```
Calculation error in measure 'Table1'[Events to Date]: The value for column 'LI Type' in table 'line_item_total' cannot be determined in the current context. Check that all columns referenced in the calculation expression exist, and that there are no circular dependencies. This can also occur when the formula for a measure refers directly to a column without performing any aggregation--such as sum, average, or count--on that column. The column does not have a single value; it has many values, one for each row of the table, and no row has been specified.
```

For example I was trying to create a calculated field that would show the "events to date":


```
=IF(Table1[LI Type]= "percent", sum(daily[Spent])*(1+Table2[Payout Percentage]),IF(Table1[LI Type]= "CPC", sum(Table1[Clicks]), if(Table1[LI Type]= "CPF", sum(Table1[Connections]))))
```

I was reading through misc. forums about creating a new calculated field for each calculation first and wrapping it in 'CALCULATE()', but I didn't have any luck with that either. What am I missing here?



*Onto my 2nd question..
*
What additional relationships or tables do I need to add so that I can create a PivotTable that combines Tables 1 & 2?
Example:

ClientLI IDLI TypeLI PayoutTierBudgetPayoutPayoutPercentFeeGoalStartDateEndDateImpressionsClicksSpentDog5CPCClicks2200,0001.40300,00012/3/123/1/131,489,11214,9548,464.57

<tbody>

</tbody>
And ultimately be able to splice this data by the Table 1 date field so I can see the line item progress over time.

Some material I read that "may" have my answer hidden in it: User-Friendly Report Sorting With Slicers! « PowerPivotPro

Sorry for the long post, and thanks in advance for any help/insights!


----------



## Mfrosty (Dec 18, 2012)

Sure thing Miguel. Give me a few to get it organized. Just got home from work. I will pm you the dropbox link.


----------

