<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Dynamic SQL Tables in Power Query</title>
    <link>https://community.fabric.microsoft.com/t5/Power-Query/Dynamic-SQL-Tables/m-p/394726#M16640</link>
    <description>&lt;P&gt;Is my understanding correct, that the table name would end with _201804 for April?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Then you could create&amp;nbsp;filter expression like this:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;DateStartOfMonth = #date(Number.From( Text.Start( Text.End( FileName, 6), 4)) , Number.From( Text.End( FileName, 2)) ,1) &lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;that returns the date of the first day in April.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;You could then compare it to the current day:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;if DateStartOfMonth &amp;gt;= Date.From(DateTime.LocalNow()) then ...&lt;/P&gt;</description>
    <pubDate>Thu, 12 Apr 2018 05:12:19 GMT</pubDate>
    <dc:creator>ImkeF</dc:creator>
    <dc:date>2018-04-12T05:12:19Z</dc:date>
    <item>
      <title>Dynamic SQL Tables</title>
      <link>https://community.fabric.microsoft.com/t5/Power-Query/Dynamic-SQL-Tables/m-p/394505#M16635</link>
      <description>&lt;P&gt;I've been trying to find a method to automatically have my sql query add new tables to it. Our database generates&amp;nbsp;a new table at the start of the month, resulting in having to update the project to now include that table. The table does end with "_YEARMO". Would it be possible to have a formula check for the first of the month and update the query to add a join for the new table?&lt;/P&gt;</description>
      <pubDate>Wed, 11 Apr 2018 20:49:30 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/Power-Query/Dynamic-SQL-Tables/m-p/394505#M16635</guid>
      <dc:creator>dstanisljevic</dc:creator>
      <dc:date>2018-04-11T20:49:30Z</dc:date>
    </item>
    <item>
      <title>Re: Dynamic SQL Tables</title>
      <link>https://community.fabric.microsoft.com/t5/Power-Query/Dynamic-SQL-Tables/m-p/394654#M16638</link>
      <description>&lt;P&gt;In theory, you could have a function that does this and essentially returns a list of all of the tables that you want and another function or the main query itself just do a union/join on that list of table names. In theory. People have done similar things with web pages that essentially have an unspecified number of pages like ?page=1, ?page=2, etc.&amp;nbsp;&lt;a href="javascript:void(0)" data-lia-user-mentions="" data-lia-user-uid="1522" data-lia-user-login="ImkeF" class="lia-mention lia-mention-user"&gt;ImkeF&lt;/a&gt; might be able to help out here.&lt;/P&gt;</description>
      <pubDate>Thu, 12 Apr 2018 03:01:03 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/Power-Query/Dynamic-SQL-Tables/m-p/394654#M16638</guid>
      <dc:creator>Greg_Deckler</dc:creator>
      <dc:date>2018-04-12T03:01:03Z</dc:date>
    </item>
    <item>
      <title>Re: Dynamic SQL Tables</title>
      <link>https://community.fabric.microsoft.com/t5/Power-Query/Dynamic-SQL-Tables/m-p/394726#M16640</link>
      <description>&lt;P&gt;Is my understanding correct, that the table name would end with _201804 for April?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Then you could create&amp;nbsp;filter expression like this:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;DateStartOfMonth = #date(Number.From( Text.Start( Text.End( FileName, 6), 4)) , Number.From( Text.End( FileName, 2)) ,1) &lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;that returns the date of the first day in April.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;You could then compare it to the current day:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;if DateStartOfMonth &amp;gt;= Date.From(DateTime.LocalNow()) then ...&lt;/P&gt;</description>
      <pubDate>Thu, 12 Apr 2018 05:12:19 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/Power-Query/Dynamic-SQL-Tables/m-p/394726#M16640</guid>
      <dc:creator>ImkeF</dc:creator>
      <dc:date>2018-04-12T05:12:19Z</dc:date>
    </item>
    <item>
      <title>Re: Dynamic SQL Tables</title>
      <link>https://community.fabric.microsoft.com/t5/Power-Query/Dynamic-SQL-Tables/m-p/399343#M16729</link>
      <description>&lt;P&gt;&lt;a href="javascript:void(0)" data-lia-user-mentions="" data-lia-user-uid="1522" data-lia-user-login="ImkeF" class="lia-mention lia-mention-user"&gt;ImkeF&lt;/a&gt;&amp;nbsp;That is correct, the April table would end in that fashion. What I'm struggling with is making that part of the select statement when querying the data from that table. I've figured out how to do it via parameters, but this only works when utilizing the desktop app.&lt;/P&gt;</description>
      <pubDate>Wed, 18 Apr 2018 15:49:13 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/Power-Query/Dynamic-SQL-Tables/m-p/399343#M16729</guid>
      <dc:creator>dstanisljevic</dc:creator>
      <dc:date>2018-04-18T15:49:13Z</dc:date>
    </item>
    <item>
      <title>Re: Dynamic SQL Tables</title>
      <link>https://community.fabric.microsoft.com/t5/Power-Query/Dynamic-SQL-Tables/m-p/399345#M16730</link>
      <description>&lt;P&gt;That surprises me: What errror-message are you getting in the service?&lt;/P&gt;</description>
      <pubDate>Wed, 18 Apr 2018 15:56:21 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/Power-Query/Dynamic-SQL-Tables/m-p/399345#M16730</guid>
      <dc:creator>ImkeF</dc:creator>
      <dc:date>2018-04-18T15:56:21Z</dc:date>
    </item>
    <item>
      <title>Re: Dynamic SQL Tables</title>
      <link>https://community.fabric.microsoft.com/t5/Power-Query/Dynamic-SQL-Tables/m-p/399444#M16734</link>
      <description>&lt;P&gt;OK, worked the variable part out and it was my error. Here's what I have that is working for a basic test:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;let&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; TableDate = "201804",&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; Source = Sql.Database("localhost", "Database", [Query="select RoomName,RoomDescription,AssetName,CFV_RawEnergyUsage_"&amp;amp;TableDate&amp;amp;".AssetID,CFV_RawEnergyUsage_"&amp;amp;TableDate&amp;amp;".EnergyUsage,CFV_RawEnergyUsage_"&amp;amp;TableDate&amp;amp;".LogTimeStamp#(lf)from CFV_RawEnergyUsage_"&amp;amp;TableDate&amp;amp;"#(lf)join CRV_Assets on CFV_RawEnergyUsage_"&amp;amp;TableDate&amp;amp;".AssetID=CRV_Assets.AssetID#(lf)join CRV_Symbols on CRV_Assets.SymbolID=CRV_Symbols.SymbolID#(lf)join CRV_Rooms on CRV_Symbols.RoomID=CRV_Rooms.RoomID"])&lt;/P&gt;&lt;P&gt;in&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; Source&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;So right now, just testing it as a fixed variable. This works and returns the table correctly. Where I'm struggling is what&amp;nbsp;&lt;a href="javascript:void(0)" data-lia-user-mentions="" data-lia-user-uid="313" data-lia-user-login="Greg_Deckler" class="lia-mention lia-mention-user"&gt;Greg_Deckler&lt;/a&gt;&amp;nbsp;suggested, looping through to bring in multiple tables. So as new tables are generated, bring there data in as well as ensuring that the past tables are in there as well.&lt;/P&gt;</description>
      <pubDate>Wed, 18 Apr 2018 19:02:03 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/Power-Query/Dynamic-SQL-Tables/m-p/399444#M16734</guid>
      <dc:creator>dstanisljevic</dc:creator>
      <dc:date>2018-04-18T19:02:03Z</dc:date>
    </item>
    <item>
      <title>Re: Dynamic SQL Tables</title>
      <link>https://community.fabric.microsoft.com/t5/Power-Query/Dynamic-SQL-Tables/m-p/401292#M16777</link>
      <description>&lt;P&gt;Here is where I'm at. I'm very new to this so I'm assuming what I'm trying to do is more complex than my knowledge of BI. So I've been able to figure out how to calculate the date info I need into the correct format and then append the tables together into a table that gives me the data I need. Where I'm failing is understanding how to loop the process. Here's what I expect the process to be:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;1) Determine start of month from current date. Grab data from that table.&lt;/P&gt;&lt;P&gt;2) Loop backwards and grab data from each table until the EndDate table is gathered.&lt;/P&gt;&lt;P&gt;3) End with all data into a single table.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Is this doable?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;let&lt;BR /&gt;EndDate = "201701",&lt;BR /&gt;TableDate1 = Date.ToText(#date(Date.Year(DateTime.LocalNow()),Date.Month(DateTime.LocalNow()),Date.Day(DateTime.LocalNow())), "yyyyMM"),&lt;BR /&gt;TableDate2 = Date.ToText(#date(Date.Year(DateTime.LocalNow()),Date.Month(DateTime.LocalNow()) - 1,Date.Day(DateTime.LocalNow())), "yyyyMM"),&lt;BR /&gt;Source1 = Sql.Database("localhost", "database", [Query="select RoomName,AssetName,CFV_SummarizedEnergyUsageByHour_"&amp;amp;TableDate1&amp;amp;".AssetID,CFV_SummarizedEnergyUsageByHour_"&amp;amp;TableDate1&amp;amp;".EnergyUsage,CFV_SummarizedEnergyUsageByHour_"&amp;amp;TableDate1&amp;amp;".LogTimeStamp#(lf)from CFV_SummarizedEnergyUsageByHour_"&amp;amp;TableDate1&amp;amp;"#(lf)join CRV_Assets on CFV_SummarizedEnergyUsageByHour_"&amp;amp;TableDate1&amp;amp;".AssetID=CRV_Assets.AssetID#(lf)join CRV_Symbols on CRV_Assets.SymbolID=CRV_Symbols.SymbolID#(lf)join CRV_Rooms on CRV_Symbols.RoomID=CRV_Rooms.RoomID"]),&lt;BR /&gt;Source2 = Sql.Database("localhost", "database", [Query="select RoomName,AssetName,CFV_SummarizedEnergyUsageByHour_"&amp;amp;TableDate2&amp;amp;".AssetID,CFV_SummarizedEnergyUsageByHour_"&amp;amp;TableDate2&amp;amp;".EnergyUsage,CFV_SummarizedEnergyUsageByHour_"&amp;amp;TableDate2&amp;amp;".LogTimeStamp#(lf)from CFV_SummarizedEnergyUsageByHour_"&amp;amp;TableDate2&amp;amp;"#(lf)join CRV_Assets on CFV_SummarizedEnergyUsageByHour_"&amp;amp;TableDate2&amp;amp;".AssetID=CRV_Assets.AssetID#(lf)join CRV_Symbols on CRV_Assets.SymbolID=CRV_Symbols.SymbolID#(lf)join CRV_Rooms on CRV_Symbols.RoomID=CRV_Rooms.RoomID"]),&lt;BR /&gt;Data = Table.Combine({Source1,Source2})&lt;BR /&gt;in&lt;BR /&gt;Data&lt;/P&gt;</description>
      <pubDate>Fri, 20 Apr 2018 21:28:06 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/Power-Query/Dynamic-SQL-Tables/m-p/401292#M16777</guid>
      <dc:creator>dstanisljevic</dc:creator>
      <dc:date>2018-04-20T21:28:06Z</dc:date>
    </item>
    <item>
      <title>Re: Dynamic SQL Tables</title>
      <link>https://community.fabric.microsoft.com/t5/Power-Query/Dynamic-SQL-Tables/m-p/401352#M16780</link>
      <description>&lt;P&gt;You don't have to loop explicitely in Power Query: Adding a column to a table in Power Query will execute a function (that you define in the last argument) that will be applied to every row. You then just expand that new column (if a table or record with multiple fields hast been created)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I've used a generic date-function from my blog that is pretty cody, so don't be scared by the amount of code, but once pasted into the advanced editor, you'll see that it's just a good handful of steps resulting ;-)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;let

	DateFunction = let 
			// ----------------------- Documentation ----------------------- 
		documentation_ = [
		Documentation.Name =  " Dates.ListDateIntervals
		", Documentation.Description = " Creates a list of dates according to the chosen interval between Start and End. Allowed values for 3rd parameter: ""Year"", ""Quarter"", ""Month"", ""Week"" or ""Day"".
		" , Documentation.LongDescription = " Creates a list of dates according to the chosen interval between Start and End. The dates created will always be at the end of the interval, so could be in the future if today is chosen.
		", Documentation.Category = " Table
		", Documentation.Source = " http://www.thebiccountant.com/2017/12/11/date-datesbetween-retrieve-dates-between-2-dates-power-bi-power-query/ . 
		", Documentation.Author = " Imke Feldmann: www.TheBIccountant.com . 
		", Documentation.Examples = {[Description =  " see http://www.thebiccountant.com/2017/12/11/date-datesbetween-retrieve-dates-between-2-dates-power-bi-power-query/ .
		" , Code = " 
		 ", Result = " 
		"]}],

		// ----------------------- Function Code ----------------------- 
		function_ =  
		(From as date, To as date, optional Selection as text ) =&amp;gt;
		let
		// Create default-value "Day" if no selection for the 3rd parameter has been made
		TimeInterval = if Selection = null then "Day" else Selection,

		// Table with different values for each case
		CaseFunctions = #table({"Case", "LastDateInTI", "TypeOfAddedTI", "NumberOfAddedTIs"},
				{   {"Day", Date.From, Date.AddDays, Number.From(To-From)+1},
					{"Week", Date.EndOfWeek, Date.AddWeeks, Number.RoundUp((Number.From(To-From)+1)/7)},
					{"Month", Date.EndOfMonth, Date.AddMonths, (Date.Year(To)*12+Date.Month(To))-(Date.Year(From)*12+Date.Month(From))+1},
					{"Quarter", Date.EndOfQuarter, Date.AddQuarters, (Date.Year(To)*4+Date.QuarterOfYear(To))-(Date.Year(From)*4+Date.QuarterOfYear(From))+1},
					{"Year", Date.EndOfYear, Date.AddYears,Date.Year(To)-Date.Year(From)+1} 
				} ),

		// Filter table on selected case
				Case = CaseFunctions{[Case = TimeInterval]},
			
		// Create list with dates: List with number of date intervals -&amp;gt; Add number of intervals to From-parameter -&amp;gt; shift dates at the end of each respective interval	
			DateFunction = List.Transform({0..Case[NumberOfAddedTIs]-1}, each Function.Invoke(Case[LastDateInTI], {Function.Invoke(Case[TypeOfAddedTI], {From, _})}))
		in
			DateFunction,
		// ----------------------- New Function Type ----------------------- 
		type_ = type function (
			  From as (type date),
			  To as (type date),
			  optional Selection as (type text meta [
									Documentation.FieldCaption = "Select Date Interval",
									Documentation.FieldDescription = "Select Date Interval, if nothing selected, the default value will be ""Day""",
									Documentation.AllowedValues = {"Day", "Week", "Month", "Quarter", "Year"}
									])
				)
			as table meta documentation_,

		// Replace the extisting type of the function with the individually defined
		Result =  Value.ReplaceType(function_, type_)
	 in 
	Result,
    
	SQL_Function = (TableDate as text) =&amp;gt;

                    Sql.Database("localhost", "database", 
                    [Query="select RoomName,AssetName,CFV_SummarizedEnergyUsageByHour_" &amp;amp; TableDate &amp;amp; ".AssetID,CFV_SummarizedEnergyUsageByHour_" &amp;amp; TableDate &amp;amp; ".EnergyUsage,CFV_SummarizedEnergyUsageByHour_" &amp;amp; TableDate &amp;amp; ".LogTimeStamp
                    from CFV_SummarizedEnergyUsageByHour_" &amp;amp; TableDate &amp;amp; "
                    join CRV_Assets on CFV_SummarizedEnergyUsageByHour_" &amp;amp; TableDate &amp;amp; ".AssetID=CRV_Assets.AssetID
                    join CRV_Symbols on CRV_Assets.SymbolID=CRV_Symbols.SymbolID
                    join CRV_Rooms on CRV_Symbols.RoomID=CRV_Rooms.RoomID"]),


    ListOfDates = DateFunction(#date(2017,01,01), Date.From(DateTime.LocalNow()), "Month"),
    #"Converted to Table" = Table.FromList(ListOfDates, Splitter.SplitByNothing(), null, null, ExtraValues.Error),
    FormattedDate = Table.AddColumn(#"Converted to Table", "Date", each Date.Year([Column1])*100 + Date.Month([Column1])),
    ChgType = Table.TransformColumnTypes(FormattedDate,{{"Date", type text}}),
    CallSQLFunction = Table.AddColumn(ChgType, "ExecuteSQLFunction", each SQL_Function([Date]))
in
    CallSQLFunction&lt;/PRE&gt;&lt;P&gt;I've also converted your SQL-Call to a function so that it can be applied to every row. Just expand the resulting column.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 21 Apr 2018 05:33:57 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/Power-Query/Dynamic-SQL-Tables/m-p/401352#M16780</guid>
      <dc:creator>ImkeF</dc:creator>
      <dc:date>2018-04-21T05:33:57Z</dc:date>
    </item>
  </channel>
</rss>

