cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
jomikk Helper I
Helper I

Help translating VBA to Power Query

Hi, I want to create a dynamic query in Power BI. But I need to change my VBA in query to Power Query such that I can use it with a parameter

 

My original MDX query contains this piece of VBA:

 

Str(Int(CDbl(Now()) - 38718))

It takes todays date and time, converts it to a number, subtracts a value needed to get right row from cube, converts it to a whole number (integer) and then finally changes it to a string so that it can be used in the query.

 

I have tried to do the following in Power Query, without luck.

 

Value = Text.From(Int64.From(Number.From(DateTime.LocalNow()) - 38718))
1 ACCEPTED SOLUTION

Accepted Solutions
jomikk Helper I
Helper I

Re: Help translating VBA to Power Query

Figured out what I did wrong!

 

I used Power Query formula inside StrToMember (which expects VBA formulaes):

 

StrToMember("[Report Date].[Report Date].&[" + Str(Int(CDbl(Now()) - 38718)) + "]")

So I changed this to:

 

[Report Date].[Report Date].&["& Value &"]

And then my original Value formula worked as I expected.

 

Value = Text.From(Int64.From(Number.From(DateTime.LocalNow()) - 38718))

But I don't know if this is the "best" way to do it, but it works

View solution in original post

3 REPLIES 3

Re: Help translating VBA to Power Query

You want something like this

 

Duration.Days(DateTime.Date(DateTime.LocalNow())-#date(2015,1,1))



* Matt is a Microsoft MVP (Power BI) and author of the Power BI Book Supercharge Power BI.
jomikk Helper I
Helper I

Re: Help translating VBA to Power Query

Cheers Matt, can you explain what this is doing? Keep in mind that I want a number, not a date.

 

Here is the cube member for context:

 

StrToMember("[Report Date].[Report Date].&[" + Str(Int(CDbl(Now()) - 38718)) + "]")

So using this VBA code for today it would yield:

 

[Report Date].[Report Date].&[3502]
jomikk Helper I
Helper I

Re: Help translating VBA to Power Query

Figured out what I did wrong!

 

I used Power Query formula inside StrToMember (which expects VBA formulaes):

 

StrToMember("[Report Date].[Report Date].&[" + Str(Int(CDbl(Now()) - 38718)) + "]")

So I changed this to:

 

[Report Date].[Report Date].&["& Value &"]

And then my original Value formula worked as I expected.

 

Value = Text.From(Int64.From(Number.From(DateTime.LocalNow()) - 38718))

But I don't know if this is the "best" way to do it, but it works

View solution in original post

Helpful resources

Announcements
New Ranks Launched March 24th!

New Ranks Launched March 24th!

The time has come: We are finally able to share more details on the brand-new ranks coming to the Power BI Community!

‘Better Together’ Contest Finalists Announced!

‘Better Together’ Contest Finalists Announced!

Congrats to the finalists of our ‘Better Together’-themed T-shirt design contest! Click for the top entries.

Arun 'Triple A' Event Video, Q&A, and Slides

Arun 'Triple A' Event Video, Q&A, and Slides

Missed the Arun 'Triple A' event or want to revisit it? We've got you covered! Check out the video, Q&A, and slides now.

Join THE global Microsoft Power Platform event series.

Join THE global Power Platform event series.

Attend for two days of expert-led learning and innovation on topics like AI and Analytics, powered by Dynamic Communities.

Community Summit North America

Community Summit North America

Innovate, Collaborate, Grow. The top training and networking event across the globe for Microsoft Business Applications

Top Solution Authors