Power BI - Table Matrix Different view - python

When you have Columns with two category (Columns_A and Columns_B)
And you have 2 measures (Value1 and Value2) (from different tables, but it doesnt matter)
Then normaly Table metrix shows like this:
But what I need is to switch columns with value in first 2 rows like this:
In other words, I need division of categories for every value.
All in One image (My dataset) :)
Do you have any idea please?
Maybe in python? (I guess)
Thanks

Create a relationship b/w both the tables with Category column and then merge both the tables by following the steps in the screenshot. (Use full outer join while merging)
and then perform unpivot operation to see the below result set.
Now in the visualization tab select the matrix and as below.

Related

2 SQL queries interlaced on ID number

I have a 2 queries that will be run repetitively to feed a report and some charts so need to make sure it is tight. First query has 25 columns and will yield out 25-50 rows from a massive table. My second query will result in another 25 columns (a couple matching columns) of 25 to 50 rows from another massive table.
Desired end result is a single document in that Query 1 (Problem) and Query 2 (Problem tasks) could match on a common column (Problem ID) so that row 1 is the problem, row 2-4 is the tasks, row 5 is the next problem and 6-9 are the tasks....ect. Now I realize I could do this manually by running the 2 queries and them just combining them in excel by hand, but looking for a eloquent process that could be reusable in my absence without too much overhead.
I was exploring inserts, union all, and cross join but the 2 queries have different columns that contain different critical data elements to be returned. Also, exploring setting up a Python job to do this by importing the CSVs and interlacing results but I am a early data science student and not yet much past creating charts from imported CSVs.
Any suggestions on how I might attack this challenge? Thanks for the help.
Picture of desired end result.
enter image description here
You can do it with something like
INSERT INTO target_table (<columns...>)
SELECT <your first query>
UNION
SELECT <your second query>
And then to retrieve data
SELECT * from target_table
WHERE <...>
ORDER BY problem_id, task_id
Just ensure both queries return the same columns, i.e. the columns you want to populate in target_table, probably using fixed default values (e.g. the first query may return a default task_id by including NULL as task_id in the column list)
Thanks for the feedback #gimix, I ended up aliasing the columns that I was able to put together from the 2 tables (open_time vs date_opened ect...) so they all matched and selected '' for the null values I needed to. I unioned the 2 selected statements as suggested, Then I finally realized I can just insert my filtering queries twice as sub queries. It will now be nice and quickly repeatable for pulling and dropping into excel 2x per week. Thank you!

python join one is to many

How do I create a one is to many relationship using python.
I have Excel Table A which has a common field (X) with Excel Table B.
The X values in Table A are unique. The X values in Table B appear multiple times.
I want to a code to go through the values in Table B and every time there is a match with table A, output a join of the row in Table C.
Someone on this forum suggested using
tableA.merge(tableB, left_on='x1', right_on='X2') but it does not work for what I require.
As an example if I have a value of 10 in the X field of Table A it can appear multiple times in table B. every time it appears in Table B i want a join done with table A.
i solved this using pd.merge(tableB,tableA, on=['X'])

How to do a nested loop type operation in Python Pandas?

I need to find a subset of a subset, and I need to do it iteratively, then at each instance of this sub-subset calculate a value and then saving it in a new outputs table.
To explain better I have a data frame similar to the one shown in the pic below;
I need to iterate through the dataset and sum the costs for all person 1 (of Group 1) for Team A (of Group 2).
Then move to person 1 in Team B and do the same, and so on until Person 1 is done.
Then move to Person 2 and do the same for all the teams again.
EXAMPLE BELOW:
My understanding was to use a nested loop something like:
for Person in Group1:
for Team in Group 2:
Newcost=sum(cost)
output.append(Person, Team, Newcost)
However, I am new to Python and pandas in particular and I am finding it difficult to use the same method I would usually due to having a data frame setting and a different syntax.
I have read about using .groupby and .loc to make my data frame smaller and group by my conditions, but I would need to do it iteratively and with two conditions at the same time, and then finally calculate my value and I am not sure how that would work.
Any suggestion would be much-appreciated Thanks!
I think, it will be easy for you to create a new dataframe instead of operating on the same dataframe. You can use dataframe.loc with the query inside to get the value of Person 1 and Person 2 with respect to the group you want. The way to use .loc can be introduced in here

Creating sub dataframes from a main one using Pandas

I wanna create sub dataframes from a main one
My main dataframe will look more or less like this one enter image description here
I wanna be able to have as a sub-dataframe like the following :
first sub dataframe
second sub dataframe
and all the rest for example in an another frame. My goal is to transform correctly my big dataframe into sub dataframe.
Any help will be appreciated, thanks :-)
You can take a rectangular section with numeric indices like this:
df.iloc[4:8, 0:8] # four rows, eight columns
Or you can use loc with column and row labels (but your data seem to be numerically labeled).

appending non-unique rows to another database using python

Hey all,
I have two databases. One with 145000 rows and approx. 12 columns. I have another database with around 40000 rows and 5 columns. I am trying to compare based on two columns values. For example if in CSV#1 column 1 says 100-199 and column two says Main St(meaning that this row is contained within the 100 block of main street), how would I go about comparing that with a similar two columns in CSV#2. I need to compare every row in CSV#1 to each single row in CSV#2. If there is a match I need to append the 5 columns of each matching row to the end of the row of CSV#2. Thus CSV#2's number of columns will grow significantly and have repeat entries, doesnt matter how the columns are ordered. Any advice on how to compare two columns with another two columns in a separate database and then iterate across all rows. I've been using python and the import csv so far with the rest of the work, but this part of the problem has me stumped.
Thanks in advance
-John
A csv file is NOT a database. A csv file is just rows of text-chunks; a proper database (like PostgreSQL or Mysql or SQL Server or SQLite or many others) gives you proper data types and table joins and indexes and row iteration and proper handling of multiple matches and many other things which you really don't want to rewrite from scratch.
How is it supposed to know that Address("100-199")==Address("Main Street")? You will have to come up with some sort of knowledge-base which transforms each bit of text into a canonical address or address-range which you can then compare; see Where is a good Address Parser but be aware that it deals with singular addresses (not address ranges).
Edit:
Thanks to Sven; if you were using a real database, you could do something like
SELECT
User.firstname, User.lastname, User.account, Order.placed, Order.fulfilled
FROM
User
INNER JOIN Order ON
User.streetnumber=Order.streetnumber
AND User.streetname=Order.streetname
if streetnumber and streetname are exact matches; otherwise you still need to consider point #2 above.

Categories

Resources