Country-Currency MDX Mapping - ssas

I'm quite new to MDX and I'm facing an issue I'm unable to solve, even after reading documentations. I have a database that contains values from all countries in all possible currencies (like e.g. Netherlands in EUR, USD, CZK, HUF, Germany in all currencies, Czech Republic in all currencies, etc.).
Now, I would like to build an MDX query in which I define pairs of country-currency values to query for and avoid having all countries with all currencies in my PowerPivot table, e.g.
Netherlands - EUR
Czech Republic - CZK
Hungary - HUF
etc.
Is that somehow possible?
Thanks and all the best

Going against the best practice, you can set that up in MDX using the concept of calculated member.
WITH MEMBER [Measures].Currency AS
CASE DimCountry.CountryName.CURRENTMEMBER
WHEN DimCountry.CountryName.&[Netherlands] THEN "Euro"
WHEN DimCountry.CountryName.&[Czech Republic] THEN "CZK"
WHEN DimCountry.CountryName.&[Hungary] THEN "HUF"
END
SELECT
DimCountry.CountryName.MEMBERS ON ROWS,
[Measures].Currency ON COLUMNS
FROM [YourCube]
The saner and better option would be add a "CurrencyOfCountry" column in your "Countries" table and populate the values there itself.

Related

Using results of one query as a combined set of conditions to run another query on the same table

I am trying to run a query to do the following; all of the information will be contained in the same table.
For a specified product of interest find the price of the product and the country it is sold in. The result of this query should produce a set of countries and a set of prices that correspond to the sales price of that product in each country (the prices will differ across countries).
For example, the result of this query may reveal that the product is sold in India for 100$ and Russia for 200$.
(In advance of running this query, we did not know the product was sold in these countries or the prices)
The results of that query, should then be used as a joined set of conditions to pull other parameters of interest from the table as per another query. The results of this query is the ultimate outcome we hope to achieve.
ie, The 2nd query should work as if it was stated where country = India AND Price = $100 (ie only IF both are true), return the name of all products that match this criteria (this will reveal alternative products at this price point in this country).
Repeat this search approach to show all products where country= Russia and price= $200 etc.
The first query should produce set of conditions and the 2nd query should loop through each of those conditions and produce the result.
The country and price combinations will differ and should not be defined statically at any point.
I have seen a few different approaches including WITH, CTE and subqueries but have struggled to do this correctly.
Part of the problem I am having is that my condition for the 2nd query is a combination of the results of the 1st query.
Any help with this is really appreciated! Thanks in advance!
You should use a cursor that will run on the first query, will keep the 2 results in 2 local variables in every iteration and then will run the second query with those variables.

Power BI dynamic column reference with DAX formula

I have a Orders and ExchangeRates tables connected via a date key:
What I am trying to do is to fetch the correct currency exchange rate from ExchangeRates, which are organized in columns, with the column name matching the currency codes from Orders.
I'm basically trying to make a dynamic column reference to the EUR or JPY columns by using the matching Orders[orderCurrency] like this:
orderExchangeRate = LOOKUPVALUE(ExchangeRates[Orders[orderCurrency]],
ExchangeRates[date],Orders[date])
or:
orderExchangeRage = SELECTCOLUMNS(ExchangeRates,Orders[orderCurrency], ....)
But none of the functions accept a dynamic column reference, they expect the actual name of the column
How can I dynamically refer to the EUR and JPY columns based on Orders[orderCurrency]? Isn't there something similar to INDIRECT to make a dynamic reference in PowerBI?
As far as I know, there is no such function as INDIRECT in DAX.
You have (at least) two options:
If it's just EUR and JPY, you could create two formulas and based on the currency switch between them with IF() or SWITCH(). Like: If (Currency = "EUR", LOOKUPVALUE(EUR), LOOKUPVALUE(JPY). Pseudo code, of course.
Unpivot the EUR and JPY column in the ExchangeRate table. Then you'll have a line for each date and currency, and you can reference it as you like. Especially usefull with more currency combinations. You can Unpivot in the Query Editor, Transformation Tab.

individual house price transactions with multiple postcodes SPARQL query

I have a list of about 900 postcodes for each solar farm in England and Wales. I would like to find the house prices for each postcode, to see how house prices may have changed after the solar farms were implemented.
I have been given a query which gives me the HPI corresponding to each postcode, but i would like to get the individual house transactions (houses sold) in each postcode.
I am new to SPARQL and have no idea how to do a single query for all the postcodes. If anyone can help it would be great.
This is the link to searching via postcode: http://landregistry.data.gov.uk/app/qonsole.
Many thanks
Reece
There is an example query there, with transactions in a postcode. It uses a VALUES clause, so simply add all your postcodes to that VALUES clause.
Note: they store postcodes as typed literals, so you'll have to append ^^xsd:string to each quoted value.
Add ?postcode to the ORDER BY clause to make a bit more sense of the result.

Access "Where" Query

I am trying to run some Queries in Access and I am unable to find a Query for this,
It is a Hotel Database and Fields Contain Room Type [Single,Double] and Location [London,USA]
Where Room Type is “Single” and Location is London 10% Tax and USA 20% Tax of Room Charge.
You can use an IIF expression:
SELECT IIF(room_type = 'Single', IIF(location = 'London', 0.1, 0.2), 0)
FROM my_table

A simple MDX question

I am new to MDX and I know that this must be a simple question but I haven't been able to find an answer.
I am modeling a a questionnaire that has questions and answers. What I am trying to achieve is to find out the number of people who gave specific answers to questions., e.g. the number of males aged between 20-25
When I run the query below for the questions individually the correct result is returned
SELECT
[Measures].[Fact Demographics Count] ON Columns
FROM
[Dsv All]
WHERE
[Answer].[Dim Answer].&[1]
[Measures].[Fact Demographics Count] is a count of the primary key column
[Answer].[Dim Answer].&[1] is the key for the Male answer
Result for number of people who are male = 150
Result for number of people who are between 20-25 = 12
But when I run the next query below rather than getting the number people who are males and aged between 20-25. I get the sum of the number of people who are males and the number of people who are between 20-25.
SELECT
[Measures].[Fact Demographics Count] ON Columns
FROM
[Dsv All]
WHERE
{[Answer].[Dim Answer].&[1],[Answer].[Dim Answer].&[9]}
result = 162
The structure of the fact table is
FactDemographicsKey,
RespodentKey,
QuestionKey,
AnswerKey
Any help would be greatly appreciated
Thanks
Take a look at the MDX function FILTER - this may give you what you need. A combination of FILTER and Member Properties to filter against the ID's might do it. You're having a problem because what you're trying to do is a little against the grain of how an OLAP cube is structured (from my experience) because Age and Gender are both members of the same dimension (Answers), which means that they each get their own cells for aggregation, but unlike if Age and Gender were each on their own dimension, they don't get aggregated with respect to one another except to get added together. In an OLAP cube, each combination of each member of each dimension with each member of every other dimension gets a "cell" with the value of each measure that is unique to that combination - that is what you want, but members of the same dimension (such as Answers) aren't cross-calculated in that way.
If possible, I would recommend breaking out the individual answers into individual dimensions, i.e. Age and Gender each have their own dimensions with their own members, then what you want to do will naturally flow out of your cube. Otherwise, I'm afraid you will have lots of MDX fiddelry to do. (I am not an MDX expert, though, so I could be completely off base on this one, but that is my understanding)
Also, definitely read the book previously mentioned, MDX Solutions, unless this is the only MDX query you think you'll need to write. MDX and Multidimensional analysis are nothing like SQL, and a solid understanding of the structure of an OLAP database and MDX in general is absolutely essential, and that book does a very, very nice job of getting you where you need to be in that department.
When trying to figure out problems with where-criteria or slices I find it helpful to breakdown the items that you're slicing on into dimensions, rather than measures.
select
[Measures].[Fact Demographics Count] on Columns
from [Dsv All]
where
{
[Answer].[Dim Answer].&[1],
[Dim Age Band].[20-25]
}
Although then you're not really using the power of MDX - you're getting just one value.
select
[Dim Answer].Members on Columns,
[Dim Age Band].Members on Rows
from [Dsv All]
where ( [Measures].[Fact Demographics Count] )
Will give you a pivot table (or crosstable) breaking down gender (on columns) by age-bands (on rows).
BTW - ff you're learning MDX this book: MDX Solutions is far and away the best starting point that I've found.
Firstly thanks to everyone for their replies. This was an interesting one to solve and for anyone new to MDX and coming from SQL its an easy trap to fall into.
So for those interested here is a brief overview of the solution.
I have 3 tables
factDemographics: holds respondents and their answers (who answered what)
dimAnswer: the answers
dimRespondent: the respondents
In the datasource view for the cube I duplicated factDemographics 5 times using Named Queries and I named these fact1, fact2, ..., fact5. (which will create 5 measure groups)
Using VS Studio's create cube wizard I set the following fact tables
fact1, fact2, ... as fact tables
dimRespondent a fact table. I use this table to get the number of respondents.
Removed the original factDemographics table.
Once the cube was created I duplicated the dimAnswer dimension 5 times, naming them filter1, filter2, ...
Finally in the Cube Structure's Dimension Usage tab I linked these together as follows
filter1 many to many dimRespondent
filter2 many to many dimRespondent
filter3 many to many dimRespondent
filter4 many to many dimRespondent
filter5 many to many dimRespondent
filter1 regular relationship fact1
filter2 regular relationship fact2
filter3 regular relationship fact3
filter4 regular relationship fact4
filter5 regular relationship fact5
This now enables me to rewrite the query I used in my original post as
SELECT
[Measures].[Dim Respondent Count] On 0
FROM
[DemographicsCube]
WHERE
(
[Filter1].[Answer].&[Male],
[Filter2].[Answer].&[20-25]
)
My query can now be filtered by up to 5 questions.
Although this works I'm sure that there is a more elegant solution. If anyone knows what that is I'd love to hear it.
Thanks
If you are using MSSQL, you can use the "WITH ROLLUP" to get some extra rows which would have the information you want. Also, you are not using a "GROUP BY" which you will need.
Use the GROUP BY to break up the set into groups and then use aggregate functions to get your counts and other stats.
Example:
select AGE, GENDER, count(1)
from MY_TABLE
group by AGE, GENDER
with rollup
This would give you the number of each gender of person in your table in each age group, and the "rollup" would give you the total number of people in your table, the numbers in each age group regardless of gender, and the numbers of each gender regardless of age. Something like
AGE GENDER COUNT
--- ------ -----
20 M 1245
21 M 1012
20 F 942
21 F 838
M 2257
F 1780
20 2187
21 1850
4037