Pivot DataFrame - python-2.7

I have a data file in this format:
I want the columns to be grouped by month in a pivot table. When I pivot the data a column for each day is being created.
df = ex.read_excel("C:\\ExportReport.xlsx", "ExportReport")
table = pd.pivot_table(df, values='Forecast Qty', rows='Part', cols='Due Date', aggfunc=np.sum, fill_value=0)
Is there a way to tell pandas to group the columns by month?

Need to have a field that calculates the month. If this is going to span multiple years, will need to combine into one field.
df['YYYY-MM'] = df['Due Date'].apply(lambda x: x.strftime("%Y-%m"))
Then try yours, but change to the monthly field...
table = pd.pivot_table(df, values='Forecast Qty', rows='Part', cols='YYYY-MM', aggfunc=np.sum, fill_value=0)

Related

Create Rows to the table where the data is missing for specific dates in power BI

I have a table with below columns in my dataset. We have monthly revenue for each resellers in this table.
For few resellers, there will be no data for some particular months, as they didn’t generate revenue on those months. I want to create rows for those resellers with the missing date and the revenue for those missing dates to be updated as blank.
Please advise how we can achieve this.
Current data:
Expected result:
For the missing dates you need to create a date table using the CALENDAR function like this:
Date table = CALENDAR(MIN(Date), MAX(Date))
This will create a table with a single colmn containing all the dates in your table with filled gaps (even if you don't have certain dates in your table). Then you need to create a relationship between your table and the date table.
When you use the date and the revenue in a visual lets say table or matrix all dates will be visible but the revenues will be blank (except for those that actually had a value in revenue).

Linking fields on Power BI

very new to Power, so sorry for asking a silly question.
I've got one table with required skills for a particular role, and a second table with a list of users and self-assessment for each of those skills.
Table1 has a 2 columns [Question] and [Required Score]
Table2 has 3 columns: [User], [Question] and [Score]
I'm trying to write a DAX formula to use as a filter: I want to return only those users where their own score is equal to or greater than the required minimum.
My current Measure is: = Table1[Question] = Table2[Question] && Table1[Required Score] <= Table2[Score]
Ask I'm typing the formula:
the tables are not being recognised
all [fields] are underlined in red
error message "A single value for column 'Question' in table 'Table1' cannot be determined.
Table1[Question] and Table2[Question] Data type = text, Format = Text, Summarization = Don't summarize, Data Category = Uncategorized
Table1[Required Score] and Table2[Score] Data type = Fixed decimal number, Format = Decimal number, Summarization = Don't summarize, Data category = Uncategorized
Can someone please explain what I'm doing wrong and point me in the direction of a fix?
Based on your tables, I'd do the following
In your Relationships, link the Question column, in Table1 & Table2
In DAX, add a new column to Table2; the formula will be something
like: Score >= RELATED(RequiredScore) i.e. a Boolean
In your visual, filter Table2 on this new column = True

Sum based on two columns from another table

I've got this table named "A" and I want to fill the Sales SUM column with the sum of sales from another table group by date and country. The other table is named "B" and got also Date, Country and Sales columns however the number of dates and countries differ. I don't want to join this tables I would like to achieve this in DAX. Is it possible?
Yes you can do that with DAX and virtual relationship:
SumSales = calculate( sum('B'[Sales])
, TREATAS(SUMMARIZE('A','A'[Date],'A'[Country]), 'B'[Date],'B'[Country])
)

HOW to write DAX to filter measures using slicer date

I have date slices with Begining and End date and I need to count the total number of employees at the beginning of the slicer date and total number of employees at the end of slicer date. Please help me in writing the DAX
Active employee = COUNTROWS('Turnover Active') need to get the number at the beginning of the slicer date and End Date
Thank you
Add a new table to your model with possible date values. For example:
TableName: DateTable
TableColumnName: DateValue (for example with values from 01.01.2019 to 06.11.2019)
Now add a slicer to your report and drag and drop the column DateValue into it.
With the following two measures you will get the start and the end date from this slicer:
MinDate = Min(DateTable[DateValue])
MaxDate = Max(DateTable[DateValue])
Now add a measure to calculate your total numbers of employees:
MinDateEmployees = Countrows(Filter(EmployeeTable; EmployeeTable[EmployeeDate] < MinDate))
MaxDateEmployees = Countrows(Filter(EmployeeTable; EmployeeTable[EmployeeDate] < MaxDate))

Power BI: need a formula to create a column from subtracting two distinct dates

I have a date for when someone last bought something (lastbuy) and have an end date (lastday) column and I want to create a new column with this new value which will be a whole number. FYI, lastday and lastbuy are both Date/Time data types. I tried the following:
Column_name = DATEDIFF(lastday, lastbuy, day)
_______________________________________________
Column_name = COUNTX('Table_name', [lastday] - [lastbuy])