Delete SQL Records Based on Derived Column Content - derived

I'm hoping someone can help me with this. I have a MS SQL table with a derived column, for date the records were imported, and need to delete records, based on the content of this column. What I need to do is delete all records from the table with a date of '2011-11-18'. Now this column is a datetime column, so it contains the time info after the date, i.e. 2011-11-18 09:29:38.000, but no matter what command I try for this:
Delete from table where Date_Imported like '2011-11-18%'
Delete from table where Date_Imported like '2011-11-18'
Delete from table where Date_Imported = 2011-11-18
It comes back saying "0 rows affected", even though I know there are records with that date in the table. Any thoughts? I'd appreciate your assistance.

If you cast it to a date, your check should work.
DELETE FROM TABLE WHERE CAST(Date_Imported As Date) = '2011-11-18'

Not sure if this is the simplest way, but it should work:
Delete from table where
year(Date_Imported) = 2011
and month(Date_Imported) = 11
and day(Date_Imported) = 18
See Date and Time Functions.

Related

Excel Power query merging different workbooks

I can't find the solution for the problem described here.
I have an Excel file with sales data of 2020 and another one with data for 2021, with lots of rows, so if I copy paste one below the other in the other Excel, I can't use pivot data because too many rows, so I want to merge my 2 Excel files in this way:
First table:
Second table:
Desired final table (in Excel):
Is there any way I can do that with power query or something else in Excel?
Note: my table doesn't have just Sales 2020 in 2021, but also other data, but for simplicity I didn't include it there (example: growth 2020, growth 2021)
So if anyone can help me I will appreciate it a lot!
I would start with 2 queries, each one would just read the rows from First table and Second table respectively.
Then I would start a new query by Reference to the First query.
In this Output query I would add a Merge Queries step, matching the first 3 columns from the First and Second queries. I would set the Join Type set to Full Outer Join.
Next in the Expand step I would return all the columns from the Second table.
Finally I would add 3 columns using the Conditional Column option, to create merged versions of Name, Surname and Month. For example:
= if [Name] = null then [Second.Name] else [Name]
Finally I would remove the un-needed columns, rename and re-order the columns if needed.

PowerBI: Comparing a filterd table against a variable string is returning an empty table

Please see the code below for a Power BI table in DAX:
TABLE1 =
VAR ParticipantOneParticipantId =
SELECTEDVALUE(
ParticipantOneDetails[ParticipantId]
)
RETURN
FILTER(
ParticipantOneMeetings,
ParticipantOneMeetings[ParticipantId] = ParticipantOneParticipantId
)
I am fetching a value for ParticipantId from a sliced table called ParticipantOneDetails and setting ParticipantOneParticipantId to it.
In the next step I am trying to filter the table ParticipantOneMeetings based on its column ParticipantId comparing it against ParticipantOneParticipantId.
The problem is that the resulting table is coming out empty even though I know that ParticipantOneParticipantId must have a value and the ParticipantOneMeetings table also has values. I verified by comparing against a hard-coded string.
Can you please point out what I am doing wrong? Is comparing this way not legal?
The problem lies in the process you are trying. A calculated/custom tables and columns are static. They always refresh when the data set is refreshed. They do not interact dynamically with the slicer value. So it is impossible to get data from a slicer dynamically for a Custom Table generation.
Now, your requirement of creating a new table based on slicer value is not completely clear to me. As what you are trying, is a simple filtered output of your table "ParticipantOneMeetings" after applying the Slicer. If you have relation between your 2 tables using column "ParticipantId", change in Slicer will automatically filter out values in ParticipantOneMeetings table. Why you wants to hold this same filtered values in a new Custom table is really a mater here to know for finding appropriate solution for you.
Turns out I needed to add the following measure to the table output:
MeetingsAttendedByBothParticipants =
countrows(
INTERSECT(
VALUES(ParticipantOneMeetings[Name]),
VALUES(ParticipantTwoMeetings[Name])
)
)
The above provides an intersection on output of two sliced meeting tables. This results in a list of meetings that both persons attend.

Default date aggregation for Excel

What is the default behavior of adding a date, time, or datetime into an Excel pivot row/column? I have seen it sometimes add it as the "raw value", sometimes it will add it as a Year > Query > Value, and other times (?) perhaps in between. For example:
When does Excel add it without aggregating it, and when does Excel aggregate it? Does it have to do with value cardinality, date range, or something else?
First, every entry in the column has to be a date/time or you won't be able to group them. In that case, obviously, the default would be not grouped.
Assuming everything is groupable, the default is no grouping. Each date will show individually.
The exception is if a pivot cache already exists. In that case it will group based on what the pivot cache says - the last way that field was grouped. This happens when you have more than one pivot table on the same data. The first pivot table creates the cache and all subsequent pivot tables use that existing cache.
In a new workbook (2010), I add a date field to the Row Labels and they are initially ungrouped by default.
I group them by month
Now I go back to the original data and make a new pivot table. I add the date field to the Column Labels.
Because it uses the same cache, it automatically has them grouped the same way. Finally, I go back to the source data and replace one of the dates with a string. If I create another pivot table, it will look like the others. But when I refresh it ungroups them because I have a non-date in there.
And if I try to Group now, it says "Cannot group that selection"
That's why it works the way it does - shared pivot cache. There are ways you can give each pivot table it's own cache but that uses more memory. However, if you want to group the same data differently, that's what you have to do.

Excel Power Query - Incremental Load from Query and adding date

I am trying to do something quite simple which I am failing to understand.
Take the output from a query, date time stamp and write it into a Excel table.
Iterate the logic again and you get the same output but the generated date time has progressed in time.
Query 1 -- From SQL which yields 2 columns category, count.
I am taking this and adding a generated date to it using DateTime.LocalNow().
Query 2 -- Target table
How can i construct a query which adds to an existing table and doesnt require me to load the result into a new table.
I have seen this blog.oraylis.de and i cant make it work since the DateTime.LocalNow() call runs for source and target and i end up with the same datetime throughout the query.
I think i am missing something obvious.
EDIT:-
= Table.Combine({SOURCE_DATA, TARGET_DATA})
This loads into a 3rd new table and doesnt take into account that 3rd table when loading - so you just end up with a new version of just the first two tables with new timestamp
These steps should work
create a query Q1 based on the SQL Statement, add your timestamp using DateTime.LocalNow() and load this into an Excel table (execute the query)
create a new query Q2 based on this Excel new table (just like that, no transforms)
Modify the first query Q1 by adding the Table.Combine with Q2 as the last step.
So, in other words, Q2 loads the existing data from the Excel table into which Q1 writes. The Excel table is always written completely but since the existing data is preserved you will get the result of new data being loaded to the table. Hope this helps.
Good luck, Hilmar

Calculate age & age buckets from a date field in powerquery

I am trying to create an ageing report from data in Power Query. I'm able to do the following if I pull the data into an Excel table:
Age Column =(TODAY()-[#[Request Date]])
Ageing buckets =LOOKUP(J19372,{-60,2.1,5.1,7.1},{"0-2","2+","5+","7+"})
However every time I refresh the data in the table from the query, I then need to copy the formulas down. This is fine for me but I want it to be automatic for others that I send the file to.
Is there a way to do those calculations in PowerQuery?
Sure you can. Except for retrieving today's date via Power Query, you need to refresh table. Check one solution for banding here at Ken Puls blog: http://www.excelguru.ca/blog/2016/02/29/creating-a-banding-function-in-power-query/

Resources