How to format a table in an Excel macro? - excel

I am trying to format a table from one format to another utilizing a macro. Please see below for an example of what I am trying to do.
From this:
User_ID FNAME LNAME ROLE_ID
1 Bob Smith Role1
1 Bob Smith Role2
1 Bob Smith Role2.2
To this
User_ID FNAME LNAME ROLE_ID ROLE_ID2
1 Bob Smith Role1 Role2, Role2.2
So I have 5 different "role categories". For example, A, B, C, D are my categories. Each category can have multiple roles assigned to a user. I want to have 5 static columns that list the different role categories and what the user is assigned from each.
If this is possible, it would be helpful to understand where to start.

Related

GSuite App Maker - how to pivot datasources

I have a simple app that takes attendance for a list of students. There are two datasources, a students table with first name, last name, id, and site, and an attendance table with first name, last name, date and present.
I want to be able to log attendance by getting a list of the students, entering a date, and check a box if they're in attendance or not (boolean column).
What I would like to do is pivot the attendance date for a new view so that instead of having a column with distinct dates, I'll have a columns for each date showing the value of the checkbox.
Ex:
Attendance 1:
First Last Date Present
Bob Smith 10/1 0
Bob Smith 10/2 1
Bob Smith 10/3 1
Kevin Brown 10/1 1
Kevin Brown 10/2 1
Kevin Brown 10/3 1
New Pivoted View:
First Last 10/1 10/2 10/3
Bob Smith 0 1 1
Kevin Brown 1 1 1
Is there a simple way to get this result in App Maker?
EDIT: For clarification. The primary purpose of the app is to capture attendance data in a classroom setting. So there is a flow where a teacher pre-populates a list of students and then checks the boxes down the line to log where a student was present/absent.
What I would like to be able to do is provide a page that presents the attendance data in a wide format so teachers can also look across the columns to see who was there on a given day.

How to merge two rows having same values into single row in python?

I am having a table called 'data' in that the values will be like following,
ID NAME DOB LOCATION
1 bob 08/10/1985 NEW JERSEY
1 bob 15/09/1987 NEW YORK
2 John 08/10/1985 NORTH CAROLINA
2 John 26/11/1990 OKLAHOMA
For example
I want output like,
ID NAME No.of.Days
1 bob difference of two given dates in days
2 John difference of two given dates in days
Please help me to form a python code to get the expected output.
If there will be only two dates in a for a given ID then below works!
df.groupby(['ID','NAME'])['DOB'].apply(lambda x: abs(pd.to_datetime(list(x)[0]) - pd.to_datetime(list(x)[1]))).reset_index(name='No.Of.Days')
Output
ID NAME No.Of.Days
0 1 bob 766 days
1 2 John 1934 days
you can use np.diff also
df.groupby(['ID','NAME'])['DOB'].apply(lambda x: np.diff(list(x))[0]).reset_index(name='No.Of.Days')
First, You need to convert Date column into date format. Lets suppose you are reading from .csv then read your .csv file as follows
df = pd.read_csv('yourfile.csv', parse_dates = ['DOB'])
otherwise, convert your existing dataframe column into date format as follows.
df['DOB'] = pd.to_datetime(df['DOB'])
now, you can perform the usual numeric operations.
df.groupby(['ID','NAME'])['DOB'].apply(lambda x: abs(pd.to_datetime(list(x)[0]) - pd.to_datetime(list(x)[1]))).reset_index(name='No.Of.Days')

Case Else Statement from different tables

I am trying to retrieve a name based on a certain type of claim. Would seem easy enough, but it's not working for me. I am working with 2 main tables, claim and exposure, and a contactname table. Each claim can have multiple exposures, but each exposure can have only one claim. Both tables contain a nameID field - claim.nameID and exposure.nameID. The nameID correlates to ContactName table.
Contact Name
ID Name
111 Jim
222 Bob
333 John
444 Sam
555 Walt
Normally, each claim can have multiple exposures and the exposures on a claim can have the same name or different names. The name ids are found on the exposure.
The exposure.nameID joins ContactName.ID
Claim claim.nameID ExposureNo exposure.nameID Name
A null 1 111 Jim
A null 2 222 Bob
A null 3 333 John
B null 1 444 Sam
B null 2 444 Sam
Workers comp claims are different. Each workers comp claim can have multiple exposures, but all the exposures have the same name. Since the exposures on a claim have the same name, the name ids are found, not on the exposure table, but the claim table. Go figure… The claim.nameID joins ContactName.ID AS ClaimContactName.
Claim claim.nameID ExposureNo exposure.nameID Name
C 111 1 null Jim
C 111 2 null Jim
C 111 3 null Jim
D 555 1 null Walt
D 555 2 null Walt
I can get the names for non workers comp claims and can get the names for workers comp claims. However, when I put the 2 queries together, I don’t get any results.
Select
CASE WHEN #Select(Claim\Policy Type) = 'Workers'' Compensation' THEN
CASE WHEN ClaimContact.Name IS NULL AND ClaimContact.Name.FirstName IS NULL THEN ClaimContact.LastName
WHEN ClaimContact.Name IS NOT NULL THEN ClaimContact.Name
ELSE ClaimContact.FirstName + Space(1) + ClaimContact.LastName END
ELSE
CASE WHEN Contact.Name IS NULL AND Contact.FirstName IS NULL THEN Contact.LastName
WHEN Contact.Name IS NOT NULL THEN Contact.Name
ELSE Contact.FirstName + Space(1) + Contact.LastName END
END
Haven’t done much of this, but could really use the help to figure this out. Thank you in advance.
This was for an SAP Universe using Information Design Tool. I ended up creating a derived table that gave me exactly what I needed, including learning something new along the way.

Lookup and return values to duplicates

I have two tables exported out of our ERP system. The data is related but one of the tables does not include the customer ID. One table is the customer master list, the other is a list of card numbers assigned to customers. For example:
Master list: ID and Name
1 Bill
2 Sam
3 Tim
Other list: Name and Card number
Bill 1234
Bill 1456
Bill 6743
Sam 3333
Tim 5555
Tim 5678
I want the customer ID to return next to each of their card number entries. Like:
1 Bill 1234
1 Bill 1456
1 Bill 6743
2 Sam 3333
3 Tim 5555
3 Tim 5678
I've been trying to do a VLOOKUP and an IF statement with VLOOKUP but I can't get the ID to display when returning a value to multiple entries.
Is there a way to do this?
Use INDEX and MATCH for this. See my example and replace my german VERGLEICH with MATCH, INDEX should be the same.

Excel - Counting unique records in a group

I'm having difficulty counting records in a file that have a unique ID and listing the number of rows associated with that specific ID.
For this file, the unique ID represents a specific family (column A). Each member of the family is in a different row with the same ID. I would like to count the number of family members(rows) in each unique family. I can have a few thousand rows so automating this would be wonderful. Thanks for any help!!
You can do this now automatically with Excel 2013.
If you have that version, then select your data to create a pivot table, and when you create your table, make sure the option 'Add this data to the Data Model' tickbox is check (see below).
Then, when your pivot table opens, create your rows, columns and values normally. Then click the field you want to calculate the distinct count of and edit the Field Value Settings:
Finally, scroll down to the very last option and choose 'Distinct Count.'
This should update your pivot table values to show the data you're looking for.
So if I'm understanding you correctly, you have something like
A B C
Fam. ID LastName FirstName
1 Smith John
1 Smith Mary
1 Smith Johnnie Jr
2 Roe Rick
3 Doe Jane
3 Doe Sam
and you want a new column (say, D), with a count of members per family:
A B C D
Fam. ID LastName FirstName Fam. Cnt
1 Smith John 3
1 Smith Mary 3
1 Smith Johnnie Jr 3
2 Roe Rick 1
3 Doe Jane 2
3 Doe Sam 2
This will do it -- insert at D2 and drag down:
=COUNTIF(A:A,A2)

Resources