View Measure definition (DAX) in excel - excel

I am fairly new to DAX and to SSAS-Tabular, so I hope that you'll forgive any ignorance.
We have an SSAS-Tabular cube and we're using it in Excel to see the data (pivot table). Is there a way for me to view the DAX behind a measure in Excel?
Thanks,
Eli

In Excel, go to the 'Data' tab, click on 'New Query', select 'from Database', select 'from SQL Server Analysis Services database'.
Put in the name of the server, and the name of the database (name of the cube).
Click the dropdown next to "MDX or DAX query (optional)".
Add the following query (note that this is DMX, not MDX or DAX, but it will work - note that if you don't edit the catalog name to match your cube, it won't return any data):
SELECT
[MEASUREGROUP_NAME] AS [Table Name],
[MEASURE_CAPTION] AS [Measure Name],
[DESCRIPTION] AS [Measure Description],
[EXPRESSION] AS [Measure Logic]
FROM
$SYSTEM.MDSCHEMA_MEASURES
WHERE
[CUBE_NAME] ='Model'
AND
[MEASURE_IS_VISIBLE]
AND
[CATALOG_NAME] = '<enter name of your cube here>'
ORDER BY
[MEASUREGROUP_NAME]
Click "Load".
You now have a page in the spreadsheet that functions as your data dictionary for measures. You can do the same thing, adding queries for dimenions with this code:
SELECT
[DIMENSION_UNIQUE_NAME] AS [Table Name],
HIERARCHY_CAPTION AS [Column Name],
[DESCRIPTION] AS [Column Description]
FROM
$system.MDSchema_hierarchies
WHERE
[CUBE_NAME] = 'Model'
AND
[HIERARCHY_ORIGIN] = 2
AND
[HIERARCHY_IS_VISIBLE]
AND
[CATALOG_NAME] = '<enter name of your cube here>'
ORDER BY
[DIMENSION_UNIQUE_NAME]
And for tables, this code:
SELECT
[DIMENSION_CAPTION] AS [Table Name],
[DESCRIPTION] AS [Table Description]
FROM
$system.MDSchema_Dimensions
WHERE
[CUBE_NAME] ='Model'
AND
[DIMENSION_CAPTION] <> 'Measures'
AND
[CATALOG_NAME] = '<enter name of your cube here>'
ORDER BY
[DIMENSION_CAPTION]
And for hierarchies, this code:
SELECT
[DIMENSION_UNIQUE_NAME] AS [Table Name],
[HIERARCHY_CAPTION] AS [Hierarchy Name],
[DESCRIPTION] AS [Hierarchy Description]
FROM
$system.MDSchema_hierarchies
WHERE
[CUBE_NAME] = 'Model'
AND
[HIERARCHY_ORIGIN] = 1
AND
[CATALOG_NAME] = '<enter name of your cube here>'
ORDER BY
[DIMENSION_UNIQUE_NAME]
If you edit the properties for these queries (use the 'Connections' button on the data tab), you can set this sheet to refresh every time the worksheet is opened (similar to how you probably have your pivot table connections set up), and now you have data dictionary tabs that automatically reflect the most current cube design. Hope this helps!

Related

ATHENA/PRESTO complex query with multiple unnested tables

i have i would like to create a join over several tables.
table login : I would like to retrieve all the data from login
table logging : calculating the Nb_of_sessions for each db & for each a specific event type by user
table meeting : calculating the Nb_of_meetings for each db & for each user
table live : calculating the Nb_of_live for each db & for each user
I have those queries with the right results :
SELECT db.id,_id as userid,firstname,lastname
FROM "logins"."login",
UNNEST(dbs) AS a1 (db)
SELECT dbid,userid,count(distinct(sessionid)) as no_of_visits,
array_join(array_agg(value.from_url),',') as from_url
FROM "loggings"."logging"
where event='url_event'
group by db.id,userid;
SELECT dbid,userid AS userid,count(*) as nb_interviews,
array_join(array_agg(interviewer),',') as interviewer
FROM "meetings"."meeting"
group by dbid,userid;
SELECT dbid,r1.user._id AS userid,count(_id) as nb_chat
FROM "lives"."live",
UNNEST(users) AS r1 (user)
group by dbid,r1.user._id;
But when i begin to try put it all together, it seems i retrieve bad data (i have only on db retrieved) and it seems not efficient.
select a1.db.id,a._id as userid,a.firstname,a.lastname,count(rl._id) as nb_chat
FROM
"logins"."login" a,
"loggings"."logging" b,
"meetings"."meeting" c,
"lives"."live" d,
UNNEST(dbs) AS a1 (db),
UNNEST(users) AS r1 (user)
where a._id = b.userid AND a._id = c.userid AND a._id = r1.user._id
group by 1,2,3,4
Do you have an idea ?
Regards.
The easiest way is to work with with to structure the subquery and then reference them.
with parameter reference:
You can use WITH to flatten nested queries, or to simplify subqueries.
The WITH clause precedes the SELECT list in a query and defines one or
more subqueries for use within the SELECT query.
Each subquery defines a temporary table, similar to a view definition,
which you can reference in the FROM clause. The tables are used only
when the query runs.
Since you already have working sub queries, the following should work:
with logins as
(
SELECT db.id,_id as userid,firstname,lastname
FROM "logins"."login",
UNNEST(dbs) AS a1 (db)
)
,visits as
(
SELECT dbid,userid,count(distinct(sessionid)) as no_of_visits,
array_join(array_agg(value.from_url),',') as from_url
FROM "loggings"."logging"
where event='url_event'
group by db.id,userid
)
,meetings as
(
SELECT dbid,userid AS userid,count(*) as nb_interviews,
array_join(array_agg(interviewer),',') as interviewer
FROM "meetings"."meeting"
group by dbid,userid
)
,chats as
(
SELECT dbid,r1.user._id AS userid,count(_id) as nb_chat
FROM "lives"."live",
UNNEST(users) AS r1 (user)
group by dbid,r1.user._id
)
select *
from logins l
left join visits v
on l.dbid = v.dbid
and l.userid = v.userid
left join meetings m
on l.dbid = m.dbid
and l.userid = m.userid
left join chats c
on l.dbid = c.dbid
and l.userid = c.userid;

'No value given for one or more required parameters' IN Operator

Using Excel 2007 VBA
Tables in Excel Sheet
using ADODB Connection and Recordset
This works:
select *
from [tblBFLC$]
where [Body ID] IN (select DISTINCT [Body ID]
from [tblBody$]
where [Luminaire Type] = 'Interior') and [LED Type] = 'COB'
This does not work:
select *
from [tblBFLC$]
where [Family ID] IN (select DISTINCT [Family ID]
from [tblFamily$]
where [Family] = 'ACLAP') and [LED Type] = 'COB'
I have checked for correct Field names.
Syntax Error:
No value given for one or more required parameters

SSIS Dynamic Excel Destination File Name as Tablename

I receive an excel file with the following info:
SQL SERVER SCHEMA TABLENAME
AdventureWorksDW Dbo DimOrganization
AdventureWorksDW Dbo ...
... ... ...
I want to export all columns information from the above TABLENAME from SQL Server into excel sheet using SSIS. The excel filename should be the TABLENAME above. One TABLENAME for one excel sheet, so the package should be dynamic.
Output: DimOrganization.xlsx (TABLENAME above, etc.)
Number Name Type Required Source
1 OrganizationKey int N AdventureWorksDW.dbo.DimOrganization
2 ParentOrganizationKey int Y AdventureWorksDW.dbo.DimOrganization
3 PercentageOfOwnership nvarchar(16) Y AdventureWorksDW.dbo.DimOrganization
4 OrganizationName nvarchar(50) Y AdventureWorksDW.dbo.DimOrganization
5 CurrencyKey int Y AdventureWorksDW.dbo.DimOrganization
In order to get the column info above from SQL, my SQL Task will be:
SELECT
ROW_NUMBER() OVER(ORDER BY (SELECT NULL)) [Number],
COLUMN_NAME Name,
DATA_TYPE +
CASE
WHEN CHARACTER_MAXIMUM_LENGTH IS NULL THEN ''
ELSE '(' + CAST(CHARACTER_MAXIMUM_LENGTH AS VARCHAR) +')'
END Type,
CASE IS_NULLABLE
WHEN 'NO' THEN 'N'
ELSE 'Y'
END AS Required,
IC.TABLE_CATALOG+'.'+IC.TABLE_SCHEMA+'.'+Ic.TABLE_NAME [Source]
FROM AdventureWorksDW.INFORMATION_SCHEMA.COLUMNS IC
WHERE TABLE_NAME = 'DimOrganization'
What should I do in SSIS to create each excel file for each TABLENAME above without using script?
I believe I need to add a variables and parameters to assign the WHERE TABLE_NAME = ? This way, if I change the TABLE_NAME variable and run it, if valid, it will retrieve the info from SQL, input metadata info into excel file and save the excel as filename? Any thought?

How to get related field value from database in odoo 11 and postgresql?

I am trying to get a related field value from database, but it showing column 'column_name' does not exist.
When i try to find out the value of product_id or using join to find the common data between sale.order and product.product Model . but it showing column 'column_name' does not exist.
In sale.order model the field defination is like
product_id = fields.Many2one('product.product', related='order_line.product_id', string='Product')
But when i try to join two table like below code to fetch all data as per product, like below code.
select coalesce(p.name,'Unassigned Product'), count(*) from sale_order o left join product_product p on o.product_id = p.id where o.state = 'sale' group by p.name;
It showing below error,
column o.product_id does not exist
LINE 1: ... from sale_order o left join product_product p on o.product_...
When i try to get data from sale_order table like below code.
select product_id from sale_order;
It showing below error.
column "product_id" does not exist
Can any one help me to get that value.
To access a related field from database , you have to use the store=True , keyword.
Rewrite your field definition as,
product_id = fields.Many2one('product.product', related='order_line.product_id', string='Product', store=True)
and uninstall and install the module.

Cannot link MS Access query with subquery

I have created a query with a subquery in Access, and cannot link it in Excel 2003: when I use the menu Data -> Import External Data -> Import Data... and select the mdb file, the query is not present in the list. If I use the menu Data -> Import External Data -> New Database Query..., I can see my query in the list, but at the end of the import wizard I get this error:
Too few parameters. Expected 2.
My guess is that the query syntax is causing the problem, in fact the query contains a subquery. So, I'll try to describe the query goal and the resulting syntax.
Table Positions
ID (Autonumber, Primary Key)
position (double)
currency_id (long) (references Currency.ID)
portfolio (long)
Table Currency
ID (Autonumber, Primary Key)
code (text)
Query Goal
Join the 2 tables
Filter by portfolio = 1
Filter by currency.code in ("A", "B")
Group by currency and calculate the sum of the positions for each currency group an call the result: sumOfPositions
Calculate abs(sumOfPositions) on each currency group
Calculate the sum of the previous results as a single result
Query
The query without the final sum can be created using the Design View. The resulting SQL is:
SELECT Currency.code, Sum(Positions.position) AS SumOfposition
FROM [Currency] INNER JOIN Positions ON Currency.ID = Positions.currency_id
WHERE (((Positions.portfolio)=1))
GROUP BY Currency.code
HAVING (((Currency.code) In ("A","B")));
in order to calculate the final SUM I did the following (in the SQL View):
SELECT Sum(Abs([temp].[SumOfposition])) AS sumAbs
FROM [SELECT Currency.code, Sum(Positions.position) AS SumOfposition
FROM [Currency] INNER JOIN Positions ON Currency.ID = Positions.currency_id
WHERE (((Positions.portfolio)=1))
GROUP BY Currency.code
HAVING (((Currency.code) In ("A","B")))]. AS temp;
So, the question is: is there a better way for structuring the query in order to make the export work?
I can't see too much wrong with it, but I would take out some of the junk Access puts in and scale down the query to this, hopefully this should run ok:
SELECT Sum(Abs(A.SumOfPosition)) As SumAbs
FROM (SELECT C.code, Sum(P.position) AS SumOfposition
FROM Currency As C INNER JOIN Positions As P ON C.ID = P.currency_id
WHERE P.portfolio=1
GROUP BY C.code
HAVING C.code In ("A","B")) As A
It might be worth trying to declare your parameters in the MS Access query definition and define their datatypes. This is especially important when you are trying to use the query outside of MS Access itself, since it can't auto-detect the parameter types. This approach is sometimes hit or miss, but worth a shot.
PARAMETERS [[Positions].[portfolio]] Long, [[Currency].[code]] Text ( 255 );
SELECT Sum(Abs([temp].[SumOfposition])) AS sumAbs
FROM [SELECT Currency.code, Sum(Positions.position) AS SumOfposition
FROM [Currency] INNER JOIN Positions ON Currency.ID = Positions.currency_id
WHERE (((Positions.portfolio)=1))
GROUP BY Currency.code
HAVING (((Currency.code) In ("A","B")))]. AS temp;
I have solved my problems thanks to the fact that the outer query is doing a trivial sum. When choosing New Database Query... in Excel, at the end of the process, after pressing Finish, an Import Data form pops up, asking
Where do you want to put the data?
you can click on Create a PivotTable report... . If you define the PivotTable properly, Excel will display only the outer sum.

Resources