Get Excel MDX Query for SSAS or SQL | Detailed 2-Steps!

Excel MDX Query: Generally, what happens is, in certain scenario we need that Query which is being runed in the backend by SSAS or SQL Server or whatever source you are using.

MDX Query?

Generally, people need this query for troubleshooting performance or collecting the traces which is being performed in the back end. For more info visit Microsoft website!

For e.g., suppose if you are using excel to fetch data from SQL server or SSAS (SQL Server Analysis Services) at that time if you select some data in your excel sheet then in the back end there is query performed by excel, where it sends all details to SQL or SSAS to get the data because SQL Server and SSAS do not understand the language of the excel. So, this query is being performed in the backend to communicate with the source.

Excel MDX Query?

People need excel mdx queries to query the database model directly to check for nay issue or problem. If you need all you excel backend queries to manupulate with data then you can follow this below steps.

  1. Open excel file and connect to the Cube/Tabular/SQL DB
  2. After selecting the required data or fields in to excel, you will get all the data reflected in your excel sheet, now perform the below tasks

Note: suppose if you’re filtering some data in the excel sheet, and it fails to load or it is time consuming then to check whether the problem is only from excel or source you can perform this task to get the MDX query and run this query at the source side directly and check the behavior.

  1. Go to home option and right click on that!
    • 1 1
  2. You will find customize the ribbon option, just click on that.
  3. At the right pan you can see developer option, enable that option.
    • 2 2

As you can see the “developer option” is added on the top of title bar of the excel. This developer option will be availibe for every user who is using excel online or on machine. Also this option will not be dependent on the office 365 editon you use.

Excel MDX Query

SSAS MDX Query?

  1. Click on that developer option as you see in the below image highlighted.
  2. You will find visual basic option in the left side, just click on that.
    • 3 2
  3. so, if you see the below image, you will find that one blank sheet will open to put, he code/script.
    • 4 2
  1. Copy this below mentioned code to get the output in to text file and paste it on the query window as shown in the below screenshot.

Code!

Sub CheckMDX()
Dim pvtTable As PivotTable
Dim fso As Object
Dim Fileout As Object
Set pvtTable = ActiveSheet.PivotTables(1)
Set fso = CreateObject(“Scripting.FileSystemObject”)
Set Fileout = fso.CreateTextFile(“C:\Temp\MDXOutput.txt“, True, True)
Fileout.Write pvtTable.MDX
Fileout.Close
End Sub

Note: C:\Temp\MDXOutput.txt is my local PC file path, mentioned your file path location here.
  1. Go to this highlighted location and you can find the MDX script in notepad file as shown in the below image.
    • More important is that always select the folder path which is not ristricted or have any GP (Group Policy) applied to it, if yes! then for sure the file will not get saved in the required folder.
Excel MDX Query

As you can see in the above image that we have successfully got out MDX Query and now I can use this query to execute directly into my Tabular or Cube model. Suppose if you have AAS (Azure Analysis Services) or SSAS (SQL Server Analysis Services) both will work with MDX queries.

Related Posts!

  1. Run as Different User option, then you can follow this article below.
  2. Create Local Admin Account in any windows, follow the below article!
  3. Could not load folder content in SSRS/PBIRS
Vickey Rajpoot
Vickey Rajpoot

Hello there! I'm dedicated Microsoft Data & AI Engineer at LTIMindtree, where I thrive technology into actionable insights. Dive into my world by visiting my YouTube channel & Webiste, "Kingfisher Tech Tips".

Articles: 77

Leave a Reply

Your email address will not be published. Required fields are marked *